Reputation: 2398
I am trying to access a single table inside of a single function using python and tornado.
From the suggestions for my previous question am trying to use Bulk
operation available in mongodb
to do so. My attempt to insert was successful but during update am getting error.
My code is
bulk = db.Test.initialize_unordered_bulk_op()
print("1")
bulk.insert({"test":"we"})
# bulk.find({"test": "we"})
bulk.update({"test": "we"},{'$set':{"test": "false"}},{'$unset':{"Cid"}})
t = bulk.execute()
print(t)
Is something wrong in the query?
And when trying to print the answer to the find
operation am getting
<tornado.concurrent.Future object at 0x02ED2630>
Can anyone guide me on how to do this bulk operation.
Upvotes: 1
Views: 1020
Reputation: 24007
If you're using Motor, you need to "yield" the Future returned by bulk.execute in order to await its completion and get a result:
@gen.coroutine
def f():
bulk = db.Test.initialize_unordered_bulk_op()
print("1")
bulk.insert({"test":"we"})
# bulk.find({"test": "we"})
bulk.update({"test": "we"},{'$set':{"test": "false"}},{'$unset':{"Cid"}})
t = yield bulk.execute()
print(t)
Motor is asynchronous; in order to wait for any Motor method that talks to the database server to complete you must either pass a callback to the method, or yield the Future the method returns.
For more information consult the tutorial:
http://motor.readthedocs.org/en/stable/examples/bulk.html#ordered-bulk-write-operations
Upvotes: 0
Reputation: 61225
You need to .find()
what you want to .update()
.
bulk.find({"test": "we"}).update({'$set': {"test": "false"}, '$unset': {"Cid", ""}})
Also you to find documents in your collection simply use the .find
method which returns a Cursor
object. You will then need to iterate over the cursor or use list
to return the documents. That being said if know that your query will return only one document then what you need is the .find_one()
method.
In your printing the result of bulk.find
doesn't make any sense because you are inserting the document, updating using the same instance of BulkOperationBuilder
which means that all your operations will be sent to the server after .execute()
Upvotes: 2