Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bulk operations in mongo update method #211

Merged
merged 1 commit into from
Jul 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions chatterbot/adapters/storage/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,31 @@ def filter(self, **kwargs):
return results

def update(self, statement):
from pymongo import UpdateOne, ReplaceOne

# Do not alter the database unless writing is enabled
if not self.read_only:
data = statement.serialize()

# Remove the text key from the data
self.statements.replace_one({'text': statement.text}, data, True)
operations = []

update_operation = ReplaceOne(
{'text': statement.text}, data, True
)
operations.append(update_operation)

# Make sure that an entry for each response is saved
for response in statement.in_response_to:

# $setOnInsert does nothing if the document is not created
self.statements.update_one(
update_operation = UpdateOne(
{'text': response.text},
{'$setOnInsert': {'in_response_to': []}},
upsert=True
)
operations.append(update_operation)

self.statements.bulk_write(operations, ordered=False)

return statement

Expand Down