Continuation to part-1 about CRUD operations.
This operation modifies an existing documents in a collection. Based on condition/criteria provided in query, it updates one or more documents in a collection.
We have different methods to update documents in a collection.
1. It modifies an existing document(s) as per condition. Either update/replace a single document or update all documents that match the condition.
Syntax:
db.<<collection_name>>.update({<<query>>},{{ <<update>>},{<< options>>})
Here,
Query: Condition/criteria to select documents for update. Means which document to update
Update: what to update in the document which we got with matching criteria.
Options:
upsert: true, if no document returns with matching criteria, it inserts document. By default this value is false.
Multi: true, update method will update all documents that matches the condition.
Note: By default, update() method will update one document only. To update all documents that match the query criteria/condition, use multi: true in options.
Ex:
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "abc", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "abc", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "abc", "item" : "aaa" }
> db.sample.update({no: 12}, {$set:{name:"asdf"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "abc", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "abc", "item" : "aaa" }
> db.sample.update({no: 12}, {$set:{name:"asdf"}},{multi: true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 2 })
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "asdf", "item" : "aaa" }
2. This method updates a single document in the collection that matches the criteria. If condition returns more than one document, it updates first document only.
Syntax:
db.<<collection_name>>.updateOne({<<filter>>},{<< update>>},{<< options>>})
Ex:
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "asdf", "item" : "aaa" }
> db.sample.updateOne({no:12},{$set:{name:"zzz", item:"abc"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "zzz", "item" : "abc" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "asdf", "item" : "aaa" }
3. This method updates all matching documents in a collection based on condition.
Syntax:
db.<<collection_name>>.updateMany({<<filter>>},{<< update>>}, {<<options>>})
Ex:
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "zzz", "item" : "abc" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "asdf", "item" : "aaa" }
> db.sample.updateMany({no:12},{$set:{name:"raja", item:"any"}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
4. This method replaces a single document in a collection that matches a condition. If condition returns more than one document, it replaces first document.
Syntax:
db.<<collection_name>>.replaceOne({<<filter>>}, {<<replacement>>},{<< options>>})
Note: It replaces the entire content of a document except ‘ _id’ field, in the second argument, give entirely new document to replace.
Ex:
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
> db.sample.replaceOne({no:12},{name: "ranga"})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.sample.find()
{ "_id" : 1, "name" : "ranga" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
This operation is used to remove documents from a collection. We can remove all documents or documents based on condition from a collection.
Syntax:
db.<<collection_name>>.remove()
OR
db.collection.remove({ <<query>>}, <justOne>)
Here,
Query: Specifies to delete documents based on condition.
justOne: set to true, to delete only one document.
Ex:
> db.sample.find()
{ "_id" : 1, "name" : "ranga" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838477"), "no" : 3, "name" : "ddd", "add" : "abc" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838478"), "no" : 4, "name" : "eee", "pin" : 12345 }
{ "_id" : ObjectId("5a355a053f46e9bcf9838479"), "no" : 5, "name" : "fff" }
> db.sample.remove({no: 4})
WriteResult({ "nRemoved" : 1 })
> db.sample.find()
{ "_id" : 1, "name" : "ranga" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838477"), "no" : 3, "name" : "ddd", "add" : "abc" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838479"), "no" : 5, "name" : "fff" }
> db.sample.remove({}, true)
WriteResult({ "nRemoved" : 1 })
> db.sample.find()
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838477"), "no" : 3, "name" : "ddd", "add" : "abc" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838479"), "no" : 5, "name" : "fff" }
> db.sample.remove({no: 12}, true)
WriteResult({ "nRemoved" : 1 })
> db.sample.find()
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838477"), "no" : 3, "name" : "ddd", "add" : "abc" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838479"), "no" : 5, "name" : "fff" }
> db.sample.remove({})
WriteResult({ "nRemoved" : 3 })
2. This method removes a single document from a collection. It deletes first document if the given condition returns multiple documents.
Syntax:
db.<<collection_name>>.deleteOne()
Ex:
> db.sample.find()
{ "_id" : ObjectId("5a365eac47f5367ba14a1651"), "no" : 1, "name" : "aaa" }
{ "_id" : ObjectId("5a365ebe47f5367ba14a1652"), "no" : 12, "name" : "bbb", "item" : "asd" }
{ "_id" : ObjectId("5a365ed247f5367ba14a1653"), "no" : 13, "name" : "ccc", "item" : "sdf" }
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
> db.sample.deleteOne({no:13})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.sample.find()
{ "_id" : ObjectId("5a365eac47f5367ba14a1651"), "no" : 1, "name" : "aaa" }
{ "_id" : ObjectId("5a365ebe47f5367ba14a1652"), "no" : 12, "name" : "bbb", "item" : "asd" }
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
> db.sample.deleteOne({no:{$lt:12}})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.sample.find()
{ "_id" : ObjectId("5a365ebe47f5367ba14a1652"), "no" : 12, "name" : "bbb", "item" : "asd" }
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
> db.sample.deleteOne({no:{$lt:15}})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.sample.find()
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
3. This method will remove multiple documents that satisfies the given condition.
Syntax:
db.<<collection_name>>.deleteMany()
Ex:
> db.sample.find()
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
{ "_id" : ObjectId("5a3660f647f5367ba14a1655"), "no" : 12, "name" : "bbb", "item" : "asd" }
{ "_id" : ObjectId("5a3660fc47f5367ba14a1656"), "no" : 1, "name" : "aaa" }
{ "_id" : ObjectId("5a36610447f5367ba14a1657"), "no" : 12, "name" : "bbb", "item" : "asd" }
> db.sample.deleteMany({no: 12})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.sample.find()
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
{ "_id" : ObjectId("5a3660fc47f5367ba14a1656"), "no" : 1, "name" : "aaa" }
Note: Its mandatory to provide criteria/condition in deleteOne() and deleteMany() methods.
-- Thiru //
UPDATE:
We have different methods to update documents in a collection.
1. It modifies an existing document(s) as per condition. Either update/replace a single document or update all documents that match the condition.
Syntax:
db.<<collection_name>>.update({<<query>>},{{ <<update>>},{<< options>>})
Here,
Query: Condition/criteria to select documents for update. Means which document to update
Update: what to update in the document which we got with matching criteria.
Options:
upsert: true, if no document returns with matching criteria, it inserts document. By default this value is false.
Multi: true, update method will update all documents that matches the condition.
Note: By default, update() method will update one document only. To update all documents that match the query criteria/condition, use multi: true in options.
Ex:
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "abc", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "abc", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "abc", "item" : "aaa" }
> db.sample.update({no: 12}, {$set:{name:"asdf"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "abc", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "abc", "item" : "aaa" }
> db.sample.update({no: 12}, {$set:{name:"asdf"}},{multi: true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 2 })
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "asdf", "item" : "aaa" }
2. This method updates a single document in the collection that matches the criteria. If condition returns more than one document, it updates first document only.
Syntax:
db.<<collection_name>>.updateOne({<<filter>>},{<< update>>},{<< options>>})
Ex:
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "asdf", "item" : "aaa" }
> db.sample.updateOne({no:12},{$set:{name:"zzz", item:"abc"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "zzz", "item" : "abc" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "asdf", "item" : "aaa" }
3. This method updates all matching documents in a collection based on condition.
Syntax:
db.<<collection_name>>.updateMany({<<filter>>},{<< update>>}, {<<options>>})
Ex:
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "zzz", "item" : "abc" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "asdf", "item" : "aaa" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "asdf", "item" : "aaa" }
> db.sample.updateMany({no:12},{$set:{name:"raja", item:"any"}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
4. This method replaces a single document in a collection that matches a condition. If condition returns more than one document, it replaces first document.
Syntax:
db.<<collection_name>>.replaceOne({<<filter>>}, {<<replacement>>},{<< options>>})
Note: It replaces the entire content of a document except ‘ _id’ field, in the second argument, give entirely new document to replace.
Ex:
> db.sample.find()
{ "_id" : 1, "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
> db.sample.replaceOne({no:12},{name: "ranga"})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.sample.find()
{ "_id" : 1, "name" : "ranga" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
DELETE:
Syntax:
db.<<collection_name>>.remove()
OR
db.collection.remove({ <<query>>}, <justOne>)
Here,
Query: Specifies to delete documents based on condition.
justOne: set to true, to delete only one document.
Ex:
> db.sample.find()
{ "_id" : 1, "name" : "ranga" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838477"), "no" : 3, "name" : "ddd", "add" : "abc" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838478"), "no" : 4, "name" : "eee", "pin" : 12345 }
{ "_id" : ObjectId("5a355a053f46e9bcf9838479"), "no" : 5, "name" : "fff" }
> db.sample.remove({no: 4})
WriteResult({ "nRemoved" : 1 })
> db.sample.find()
{ "_id" : 1, "name" : "ranga" }
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838477"), "no" : 3, "name" : "ddd", "add" : "abc" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838479"), "no" : 5, "name" : "fff" }
> db.sample.remove({}, true)
WriteResult({ "nRemoved" : 1 })
> db.sample.find()
{ "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838477"), "no" : 3, "name" : "ddd", "add" : "abc" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838479"), "no" : 5, "name" : "fff" }
> db.sample.remove({no: 12}, true)
WriteResult({ "nRemoved" : 1 })
> db.sample.find()
{ "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "raja", "item" : "any" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838477"), "no" : 3, "name" : "ddd", "add" : "abc" }
{ "_id" : ObjectId("5a355a053f46e9bcf9838479"), "no" : 5, "name" : "fff" }
> db.sample.remove({})
WriteResult({ "nRemoved" : 3 })
2. This method removes a single document from a collection. It deletes first document if the given condition returns multiple documents.
Syntax:
db.<<collection_name>>.deleteOne()
Ex:
> db.sample.find()
{ "_id" : ObjectId("5a365eac47f5367ba14a1651"), "no" : 1, "name" : "aaa" }
{ "_id" : ObjectId("5a365ebe47f5367ba14a1652"), "no" : 12, "name" : "bbb", "item" : "asd" }
{ "_id" : ObjectId("5a365ed247f5367ba14a1653"), "no" : 13, "name" : "ccc", "item" : "sdf" }
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
> db.sample.deleteOne({no:13})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.sample.find()
{ "_id" : ObjectId("5a365eac47f5367ba14a1651"), "no" : 1, "name" : "aaa" }
{ "_id" : ObjectId("5a365ebe47f5367ba14a1652"), "no" : 12, "name" : "bbb", "item" : "asd" }
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
> db.sample.deleteOne({no:{$lt:12}})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.sample.find()
{ "_id" : ObjectId("5a365ebe47f5367ba14a1652"), "no" : 12, "name" : "bbb", "item" : "asd" }
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
> db.sample.deleteOne({no:{$lt:15}})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.sample.find()
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
3. This method will remove multiple documents that satisfies the given condition.
Syntax:
db.<<collection_name>>.deleteMany()
Ex:
> db.sample.find()
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
{ "_id" : ObjectId("5a3660f647f5367ba14a1655"), "no" : 12, "name" : "bbb", "item" : "asd" }
{ "_id" : ObjectId("5a3660fc47f5367ba14a1656"), "no" : 1, "name" : "aaa" }
{ "_id" : ObjectId("5a36610447f5367ba14a1657"), "no" : 12, "name" : "bbb", "item" : "asd" }
> db.sample.deleteMany({no: 12})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.sample.find()
{ "_id" : ObjectId("5a365edf47f5367ba14a1654"), "no" : 13, "name" : "ccc", "item" : "sdf" }
{ "_id" : ObjectId("5a3660fc47f5367ba14a1656"), "no" : 1, "name" : "aaa" }
Note: Its mandatory to provide criteria/condition in deleteOne() and deleteMany() methods.
-- Thiru //