I have mongo structure like this:
{
"_id" : ObjectId("51596b7e469b9c3816000001"),
"company" : {
"_id" : "ade2fd0ec9b8b5e9152e0155",
"title" : "LO3426546457"
},
}
{
"_id" : ObjectId("51596cef469b9c3816000008"),
"company" : {
"_id" : "ade2fd0ec9b8b5e9152e0155",
"title" : "LO3426546457"
},
}
{
"_id" : ObjectId("51596cc3469b9c3816000007"),
"company" : {
"_id" : "ade2fd0ec9b8b5e9152e0155",
"title" : "LO3426546457"
}
}
And I want to change all 'title' fields for objects with a specific '_id'. I do like this:
Collections.UsersCollection.update({
'company._id': 'ade2fd0ec9b8b5e9152e0155'
}, {
$set: {
'company': { _id: 'ade2fd0ec9b8b5e9152e0155', title: 'blablabla' }
// I also tried: 'company.title': 'blablabla'
}
}, false, true);
And after execution that code in Node.js (I use node-mongodb-native), mongo updates only one document.
But if I do that command at Mongo Shell (mongo.exe), everything works fine and updates all documents.
What's the problem?
By default mongo updates only a single document. http://docs.mongodb.org/manual/reference/method/db.collection.update/
Use multi option for multiple document update.
By default collection.update() updates only a single document, if you need to update multiple docs you have to add the {multi:1} parameter before the callback function.