MongoDB find and remove - the fastest way

前端 未结 4 956
忘掉有多难
忘掉有多难 2021-02-07 13:56

I have a quick question, what is the fast way to grab and delete an object from a mongo collection. Here is the code, I have currently:

$cursor = $coll->find(         


        
4条回答
  •  面向向阳花
    2021-02-07 14:28

    Use the findAndRemove function as documented here: http://api.mongodb.org/java/current/com/mongodb/DBCollection.html

    The findAndRemove function retrieve and object from the mongo database and delete it in a single (atomic) operation.

    findAndRemove(query, sort[, options], callback)

    • The query object is used to retrieve the object from the database (see collection.find())
    • The sort parameter is used to sort the results (in case many where found)

提交回复
热议问题