Insert () method
To insert data into a MongoDB collection, you need to use the MongoDB Insert () or Save () method.
Grammar
The basic syntax for the Insert () command is as follows:
>db. Collection_name. Insert(document)
Example
>Db.MyCol.Insert({_id: ObjectId(7df78ad8902c),Title: ' MongoDB Overview ', Description: ' MongoDB is no SQL database ' Span class= "pun" >, by: , Url: , Tags: [ ' MongoDB ' , ' database ' , ' NoSQL ' ], Likes: 100})
Here MyCol is the name of the collection, as created in the previous tutorial. If the collection does not exist in the database, MongoDB creates the collection and then inserts it into the document.
Insert the document, if we do not specify the _id parameter, then mongodb this document assigns a unique objectid.
_ID is a 12-byte hexadecimal number that is unique to each 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)
To insert multiple documents into a single query, you can pass a file of an array insert () command.
Example
>Db.Post.Insert([{Title: ' MongoDB Overview ',Description: ' MongoDB is no SQL database ', By: ' Tutorials Point ',Url: ' Http://www.yiibai.com ',Tags: [' MongoDB ', ' Database ', ' NoSQL '],Likes: 100},{Title: ' NoSQL Database ',Description: ' NoSQL database doesn 't have tables' By: 'Tutorials Point', url: 'http:Www.yiibai.com ',Tags: [' MongoDB ', ' Database ', ' NoSQL '],Likes: 20,Comments: [ {User: ' user1 ' , Message: span class= "str" > ' My first comment ' , Datecreated: new date ( 2013,11,10 ,2,35), Like: 0} ]}< Span class= "pun")
To insert a file, you can also use Db.post.save (document). If _id is not specified in the document, then its save () method works the same as the insert () method. If _id is specified, it replaces the entire data file, which contains the _id specified save () method.
MongoDB Tutorial Lesson Sixth MongoDB Insert Document