Lets learn how to update all the documents present in a collection using update() method, using the option multi: true
test database, names collection
> db.names.find().pretty()
{
"_id" : ObjectId("53c6392a2eea8062e084cb57"),
"Company" : "Google",
"Product" : "Nexus",
"No" : 1
}
{
"_id" : ObjectId("53c639392eea8062e084cb58"),
"Company" : "Apple",
"Product" : "Mac",
"No" : 2
}
{
"_id" : ObjectId("53c63b26b003603dfdcf8c52"),
"Company" : "Xiaomi",
"Product" : "Mi3",
"No" : 3
}
{
"_id" : ObjectId("53c63bd1b003603dfdcf8c53"),
"Product" : "Smart Watch",
"No" : 4,
"Company" : "Sony"
}
We have 4 documents in “names” collection.
find() method
> db.names.find({}, {"Company": 1, "_id": 0}).pretty()
{ "Company" : "Google" }
{ "Company" : "Apple" }
{ "Company" : "Xiaomi" }
{ "Company" : "Sony" }
If we leave the first argument of find() method empty, it matches with all the documents of the collection. Hence it fetched all the Company names from the documents.
Multi-Update: MongoDB
update() method
> db.names.update({}, {$set: {"IT": "true"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.names.find().pretty()
{
"_id" : ObjectId("53c6392a2eea8062e084cb57"),
"Company" : "Google",
"Product" : "Nexus",
"No" : 1,
"IT" : "true"
}
{
"_id" : ObjectId("53c639392eea8062e084cb58"),
"Company" : "Apple",
"Product" : "Mac",
"No" : 2
}
{
"_id" : ObjectId("53c63b26b003603dfdcf8c52"),
"Company" : "Xiaomi",
"Product" : "Mi3",
"No" : 3
}
{
"_id" : ObjectId("53c63bd1b003603dfdcf8c53"),
"Product" : "Smart Watch",
"No" : 4,
"Company" : "Sony"
}
But in case of update() method, if the first argument is left empty, it randomly matches to only 1 document in the collection.
Related Read: Update with SET Operator: MongoDB
unset the IT field
> db.names.update({"IT": {$exists: true}}, {$unset: {"IT": "true"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.names.find().pretty()
{
"_id" : ObjectId("53c6392a2eea8062e084cb57"),
"Company" : "Google",
"Product" : "Nexus",
"No" : 1
}
{
"_id" : ObjectId("53c639392eea8062e084cb58"),
"Company" : "Apple",
"Product" : "Mac",
"No" : 2
}
{
"_id" : ObjectId("53c63b26b003603dfdcf8c52"),
"Company" : "Xiaomi",
"Product" : "Mi3",
"No" : 3
}
{
"_id" : ObjectId("53c63bd1b003603dfdcf8c53"),
"Product" : "Smart Watch",
"No" : 4,
"Company" : "Sony"
}
Here we select the document which has a field called “IT” and remove it from that document.
Related Read:
$exists, $type, $regex operators: MongoDB
Update with UNSET Operator: MongoDB
multi-update true
> db.names.update({}, {$set: {"IT": "true"}}, {multi: true});
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
> db.names.find().pretty()
{
"_id" : ObjectId("53c6392a2eea8062e084cb57"),
"Company" : "Google",
"Product" : "Nexus",
"No" : 1,
"IT" : "true"
}
{
"_id" : ObjectId("53c639392eea8062e084cb58"),
"Company" : "Apple",
"Product" : "Mac",
"No" : 2,
"IT" : "true"
}
{
"_id" : ObjectId("53c63b26b003603dfdcf8c52"),
"Company" : "Xiaomi",
"Product" : "Mi3",
"No" : 3,
"IT" : "true"
}
{
"_id" : ObjectId("53c63bd1b003603dfdcf8c53"),
"Product" : "Smart Watch",
"No" : 4,
"Company" : "Sony",
"IT" : "true"
}
Here we have 3 arguments for update() method. First one is intentionally left empty. In second argument, we specify the changes needed to the documents. In third argument we specify the option, multi: true – which tells mongo engine to match with all the documents in the collection.
View Comments
multi-operator works only with $ operators