Your cart is currently empty!
Author: alien
-
Khóa học miễn phí MongoDB – Update Document nhận dự án làm có lương
MongoDB – Update Document
MongoDB”s update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.
MongoDB Update() Method
The update() method updates the values in the existing document.
Syntax
The basic syntax of update() method is as follows −
>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
Example
Consider the mycol collection has the following data.
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
Following example will set the new title ”New MongoDB Tutorial” of the documents whose title is ”MongoDB Overview”.
>db.mycol.update({''title'':''MongoDB Overview''},{$set:{''title'':''New MongoDB Tutorial''}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) >db.mycol.find() { "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"} >
By default, MongoDB will update only a single document. To update multiple documents, you need to set a parameter ”multi” to true.
>db.mycol.update({''title'':''MongoDB Overview''}, {$set:{''title'':''New MongoDB Tutorial''}},{multi:true})
MongoDB Save() Method
The save() method replaces the existing document with the new document passed in the save() method.
Syntax
The basic syntax of MongoDB save() method is shown below −
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
Example
Following example will replace the document with the _id ”5983548781331adf45ec5”.
>db.mycol.save( { "_id" : ObjectId("507f191e810c19729de860ea"), "title":"Tutorials Point New Topic", "by":"Tutorials Point" } ) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : ObjectId("507f191e810c19729de860ea") }) >db.mycol.find() { "_id" : ObjectId("507f191e810c19729de860e6"), "title":"Tutorials Point New Topic", "by":"Tutorials Point"} { "_id" : ObjectId("507f191e810c19729de860e6"), "title":"NoSQL Overview"} { "_id" : ObjectId("507f191e810c19729de860e6"), "title":"Tutorials Point Overview"} >
MongoDB findOneAndUpdate() method
The findOneAndUpdate() method updates the values in the existing document.
Syntax
The basic syntax of findOneAndUpdate() method is as follows −
>db.COLLECTION_NAME.findOneAndUpdate(SELECTIOIN_CRITERIA, UPDATED_DATA)
Example
Assume we have created a collection named empDetails and inserted three documents in it as shown below −
> db.empDetails.insertMany( [ { First_Name: "Radhika", Last_Name: "Sharma", Age: "26", e_mail: "radhika_sharma.123@gmail.com", phone: "9000012345" }, { First_Name: "Rachel", Last_Name: "Christopher", Age: "27", e_mail: "Rachel_Christopher.123@gmail.com", phone: "9000054321" }, { First_Name: "Fathima", Last_Name: "Sheik", Age: "24", e_mail: "Fathima_Sheik.123@gmail.com", phone: "9000054321" } ] )
Following example updates the age and email values of the document with name ”Radhika”.
> db.empDetails.findOneAndUpdate( {First_Name: ''Radhika''}, { $set: { Age: ''30'',e_mail: ''radhika_newemail@gmail.com''}} ) { "_id" : ObjectId("5dd6636870fb13eec3963bf5"), "First_Name" : "Radhika", "Last_Name" : "Sharma", "Age" : "30", "e_mail" : "radhika_newemail@gmail.com", "phone" : "9000012345" }
MongoDB updateOne() method
This methods updates a single document which matches the given filter.
Syntax
The basic syntax of updateOne() method is as follows −
>db.COLLECTION_NAME.updateOne(<filter>, <update>)
Example
> db.empDetails.updateOne( {First_Name: ''Radhika''}, { $set: { Age: ''30'',e_mail: ''radhika_newemail@gmail.com''}} ) { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 0 } >
MongoDB updateMany() method
The updateMany() method updates all the documents that matches the given filter.
Syntax
The basic syntax of updateMany() method is as follows −
>db.COLLECTION_NAME.update(<filter>, <update>)
Example
> db.empDetails.updateMany( {Age:{ $gt: "25" }}, { $set: { Age: ''00''}} ) { "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
You can see the updated values if you retrieve the contents of the document using the find method as shown below −
> db.empDetails.find() { "_id" : ObjectId("5dd6636870fb13eec3963bf5"), "First_Name" : "Radhika", "Last_Name" : "Sharma", "Age" : "00", "e_mail" : "radhika_newemail@gmail.com", "phone" : "9000012345" } { "_id" : ObjectId("5dd6636870fb13eec3963bf6"), "First_Name" : "Rachel", "Last_Name" : "Christopher", "Age" : "00", "e_mail" : "Rachel_Christopher.123@gmail.com", "phone" : "9000054321" } { "_id" : ObjectId("5dd6636870fb13eec3963bf7"), "First_Name" : "Fathima", "Last_Name" : "Sheik", "Age" : "24", "e_mail" : "Fathima_Sheik.123@gmail.com", "phone" : "9000054321" } >
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
Khóa học miễn phí MongoDB – Insert Document nhận dự án làm có lương
MongoDB – Insert Document
In this chapter, we will learn how to insert document in MongoDB collection.
The insert() Method
To insert data into MongoDB collection, you need to use MongoDB”s insert() or save() method.
Syntax
The basic syntax of insert() command is as follows −
>db.COLLECTION_NAME.insert(document)
Example
> db.users.insert({ ... _id : ObjectId("507f191e810c19729de860ea"), ... title: "MongoDB Overview", ... description: "MongoDB is no sql database", ... by: "tutorials point", ... url: "http://www.tutorialspoint.com", ... tags: [''mongodb'', ''database'', ''NoSQL''], ... likes: 100 ... }) WriteResult({ "nInserted" : 1 }) >
Here mycol is our collection name, as created in the previous chapter. If the collection doesn”t exist in the database, then MongoDB will create this collection and then insert a document into it.
In the inserted document, if we don”t specify the _id parameter, then MongoDB assigns a unique ObjectId for this document.
_id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are divided as follows −
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)
You can also pass an array of documents into the insert() method as shown below:.
> db.createCollection("post") > db.post.insert([ { title: "MongoDB Overview", description: "MongoDB is no SQL database", by: "tutorials point", url: "http://www.tutorialspoint.com", tags: ["mongodb", "database", "NoSQL"], likes: 100 }, { title: "NoSQL Database", description: "NoSQL database doesn''t have tables", by: "tutorials point", url: "http://www.tutorialspoint.com", tags: ["mongodb", "database", "NoSQL"], likes: 20, comments: [ { user:"user1", message: "My first comment", dateCreated: new Date(2013,11,10,2,35), like: 0 } ] } ]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) >
To insert the document you can use db.post.save(document) also. If you don”t specify _id in the document then save() method will work same as insert() method. If you specify _id then it will replace whole data of document containing _id as specified in save() method.
The insertOne() method
If you need to insert only one document into a collection you can use this method.
Syntax
The basic syntax of insert() command is as follows −
>db.COLLECTION_NAME.insertOne(document)
Example
Following example creates a new collection named empDetails and inserts a document using the insertOne() method.
> db.createCollection("empDetails") { "ok" : 1 }
> db.empDetails.insertOne( { First_Name: "Radhika", Last_Name: "Sharma", Date_Of_Birth: "1995-09-26", e_mail: "radhika_sharma.123@gmail.com", phone: "9848022338" }) { "acknowledged" : true, "insertedId" : ObjectId("5dd62b4070fb13eec3963bea") } >
The insertMany() method
You can insert multiple documents using the insertMany() method. To this method you need to pass an array of documents.
Example
Following example inserts three different documents into the empDetails collection using the insertMany() method.
> db.empDetails.insertMany( [ { First_Name: "Radhika", Last_Name: "Sharma", Date_Of_Birth: "1995-09-26", e_mail: "radhika_sharma.123@gmail.com", phone: "9000012345" }, { First_Name: "Rachel", Last_Name: "Christopher", Date_Of_Birth: "1990-02-16", e_mail: "Rachel_Christopher.123@gmail.com", phone: "9000054321" }, { First_Name: "Fathima", Last_Name: "Sheik", Date_Of_Birth: "1990-02-16", e_mail: "Fathima_Sheik.123@gmail.com", phone: "9000054321" } ] ) { "acknowledged" : true, "insertedIds" : [ ObjectId("5dd631f270fb13eec3963bed"), ObjectId("5dd631f270fb13eec3963bee"), ObjectId("5dd631f270fb13eec3963bef") ] } >
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc