I have one now.nums
Set, inside the elements are as follows:
\> db.nums.find()
{ "_id" : ObjectId("599c3f530686a4fe90ef5a6f"), "n" : 1 }
{ "_id" : ObjectId("599c3f530686a4fe90ef5a70"), "n" : 2 }
Define a query document and find all documents with n not equal to 5:
> var query={n:{$ne:5}}
Then try usingfind
Function query:
> db.nums.find(query)
{ "_id" : ObjectId("599c3f530686a4fe90ef5a6f"), "n" : 1 }
{ "_id" : ObjectId("599c3f530686a4fe90ef5a70"), "n" : 2 }
It is normal to find out that there are two results.
But then I want to change all the n that meet the requirements to 5, and use it according to my idea.$ne
You should be able to filter out two results so that the N of both documents should be 5, but:
> db.nums.update(query,{$set:{n:5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.nums.find()
{ "_id" : ObjectId("599c3f530686a4fe90ef5a6f"), "n" : 5 }
{ "_id" : ObjectId("599c3f530686a4fe90ef5a70"), "n" : 2 }
It seems that only the first n is changed to 5, while the second one is not, but the second one is also satisfied.query
What are the query criteria for?
Update can only change the first result?
Read the document carefully.
update
In the history of the method, only one document is modified by default. If you want to modify multiple documents that meet the criteria, you should use parameters.{multi: true}
:> db.nums.update(query,{$set:{n:5}}, {multi: true});
Later, in order to prevent confusion, it was added
updateOne
/updateMany
Method, so it can also be written as:> db.nums.updateMany(query,{$set:{n:5}});