Search code examples
mongodbtornado

how can i do lot of operation on mongodb document?


here is my schema:

"_id" : "[email protected]"

"modif" : 1

"prs" :
{
"pass" : "sdfdsf"
}

"reset" :
[
{
"code" : "1680"
"timr" : "AQQOAAMGCQMcBgodAAYYAgIMDAkLBAEeAAcNAAEG"
}
]

i want to execute lot of operation IN THE SAME LINE OF CODE

yield tornado.gen.Task(db.users.update, {"_id":email}, {"$set":{"prs.pass":password}}, {"$inc":{"modif":1}}, {"$pop":{"reset":1}})

and i got this error:

assert isinstance(upsert, bool), "upsert must be an instance of bool"
AssertionError: upsert must be an instance of bool

but when i split the operation into THREE it works,

yield tornado.gen.Task(db.users.update, {"_id":email}, {"$set":{"prs.pass":password}})
yield tornado.gen.Task(db.users.update, {"_id":email}, {"$pop":{"reset":1}})
yield tornado.gen.Task(db.users.update, {"_id":email}, {"$inc":{"modif":1}})

i found this but thought that it is not the same thing in asynchronous calls, is this mongodb lock?

Update: tried this and worked:

yield [tornado.gen.Task(db.users.update, {"_id":email}, {"$set":{"prs.pass":password}}),
      tornado.gen.Task(db.users.update, {"_id": email}, {"$pop":{"reset":1}}),
      tornado.gen.Task(db.users.update, {"_id":email}, {"$inc":{"modif":1}})]

but isent dangerous?


Solution

  • You can pass multiple operators in the dict provided to the document argument of update:

    yield tornado.gen.Task(db.users.update, {"_id":email}, {"$set":{"prs.pass":password}, "$inc":{"modif":1}, "$pop":{"reset":1}})