MongoDB Update() method
MongoDB's update() method is used to update document into a collection. The update() method update values in the existing document.
> db.democol.find().pretty()
Result:
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
{
"_id" : ObjectId("52963ce96f63f810a98b7a99"),
"by" : "subramanian",
"comments" : [
{
"user" : "Tiara",
"message" : "Worth to read"
}
],
"likes" : 20,
"title" : "Basic SQL"
}
> db.democol.update({'title':'Basic SQL'},{$set:{'title':'Learn SQL'}})
> db.democol.find().pretty()
Result:
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
{
"_id" : ObjectId("52963ce96f63f810a98b7a99"),
"by" : "subramanian",
"comments" : [
{
"user" : "Tiara",
"message" : "Worth to read"
}
],
"likes" : 20,
"title" : "Learn SQL"
}
By default mongodb will update only single document, to update multiple you need to set a paramter 'multi' to true.
>db.democol.update({'title':'Basic SQL'},{$set:{'title':'Learn SQL'}},{multi:true})
The remove() Method
MongoDB's remove() method is used to remove document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag
deletion criteria : (Optional) deletion criteria according to documents will be removed.
justOne : (Optional) if set to true or 1, then remove only one document.
>db.mycol.find()
Result:
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
Following example will remove all the documents whose title is 'MongoDB Overview'
>db.mycol.remove({'title':'MongoDB Overview'})
>db.mycol.find()
Result:
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
Remove only one
If there are multiple records and you want to delete only first record, then set justOne parameter in remove() method
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
MongoDB Projection
In mongodb projection meaning is selecting only necessary data rather than selecting whole of the data of a document. If a document has 5 fields and you need to show only 3, then select only 3 fields from them.
The find() Method
MongoDB's find() method, explained in MongoDB Query Document accepts second optional parameter that is list of fields that you want to retrieve. In MongoDB when you execute find() method, then it displays all fields of a document. To limit this you need to set list of fields with value 1 or 0. 1 is used to show the filed while 0 is used to hide the field.
> db.democol.find({},{title:1,_id:0})
Result:
{ "title" : "Learn mongo" }
{ "title" : "Learn SQL" }
MongoDB's update() method is used to update document into a collection. The update() method update values in the existing document.
> db.democol.find().pretty()
Result:
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
{
"_id" : ObjectId("52963ce96f63f810a98b7a99"),
"by" : "subramanian",
"comments" : [
{
"user" : "Tiara",
"message" : "Worth to read"
}
],
"likes" : 20,
"title" : "Basic SQL"
}
> db.democol.update({'title':'Basic SQL'},{$set:{'title':'Learn SQL'}})
> db.democol.find().pretty()
Result:
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
{
"_id" : ObjectId("52963ce96f63f810a98b7a99"),
"by" : "subramanian",
"comments" : [
{
"user" : "Tiara",
"message" : "Worth to read"
}
],
"likes" : 20,
"title" : "Learn SQL"
}
By default mongodb will update only single document, to update multiple you need to set a paramter 'multi' to true.
>db.democol.update({'title':'Basic SQL'},{$set:{'title':'Learn SQL'}},{multi:true})
The remove() Method
MongoDB's remove() method is used to remove document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag
deletion criteria : (Optional) deletion criteria according to documents will be removed.
justOne : (Optional) if set to true or 1, then remove only one document.
>db.mycol.find()
Result:
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
Following example will remove all the documents whose title is 'MongoDB Overview'
>db.mycol.remove({'title':'MongoDB Overview'})
>db.mycol.find()
Result:
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
Remove only one
If there are multiple records and you want to delete only first record, then set justOne parameter in remove() method
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
MongoDB Projection
In mongodb projection meaning is selecting only necessary data rather than selecting whole of the data of a document. If a document has 5 fields and you need to show only 3, then select only 3 fields from them.
The find() Method
MongoDB's find() method, explained in MongoDB Query Document accepts second optional parameter that is list of fields that you want to retrieve. In MongoDB when you execute find() method, then it displays all fields of a document. To limit this you need to set list of fields with value 1 or 0. 1 is used to show the filed while 0 is used to hide the field.
> db.democol.find({},{title:1,_id:0})
Result:
{ "title" : "Learn mongo" }
{ "title" : "Learn SQL" }