javascript - MongoDB: incorrect update count -
In my database, I have a field called 'fruits' which is a simple array i when this array In the element, I use the $ addToSet
to insert only those components that are not already present.
I want to know that the element that I have inserted actually revises this area. However, docModified
parameter always returns 1 return in the callback, whenever I try to add an element that already exists .
model.update ({user name: user name}, // document {$ addToSet: {fruit: "apple"}}, // Add "apple" for fruit field function, DocModified] {console.log (docModified); // problem: this will print "1", whatever it is); Do anyone know why? Thanks a lot! (Using BTW I am using Mongoose)
Existing method implementation in the use of legacy writing concern API Do the calculation of revised documents as you note, even if there is no real change in the material, such as an operation that does not add a new member to the set, the revised count will be returned.
Unless your MongoDB server version is recently enough (the mogodydi should be 2.6 or higher) and your mangouz version is recently bundled enough for a mongodb driver, then you get the correct number from the reactions You can. Call the .collection
accelerator on the model, which returns the original driver store object:
var bulk = Model.collection.initializeOlderedBulkOp ( ); Bulk.find ({username: username}} .updateOne ({$ addToSet: {fruit: "apple"}}); Bulk.execute (function (mistake, result) {If (mistake) is fault; console.log (JSON.stringify (result, undefined, 4))}}
"result" The one who came back is the object that is in accordance with the specification), which will appear more or less like this:
{"writeErrors": [], "Enter to write": [ ], "NIndsted": 2, "NUPSTED": 0, "NMATED": 3, "NMODIFED": 3, "NRMD": 1, "UPSTED": []}
But in particular, where you use $ addToSet
and the member already exists, please In the code, it will be "nMateched": 1 and "nmodified": 0
, which is the result you want to see, to ensure that nothing was added in the set .
In the Mongodaby 2.6 shell, all updates and insertion methods try to use this API where it is available and legacy implementation only when the fallback connecting to the old server, so if you have a modern server If you do this with, you will see the same results.
Hopefully, the self will also be altered to use these methods, where available and provide access to the same reaction. It seems like a future, but the codebase is not yet to capture and use it. Note
Note : After reaching the archive object to take precautions, the basic driver methods are to ensure that from time to time it is available from databases near Mangoloj Monooses hides any request by queuing any requests until a connection is actually installed.
So if you are going directly to the collection object methods, then you can be sure that you are waiting for an "open" event before executing that code on a connection.
Comments
Post a Comment