Insert Document in MongoDB

Insert () method in MongoDB

To insert data into Collection in MongoDB, you need to use the insert () or save () method .

Syntax

The basic syntax of insert () is as follows:

 > db . COLLECTION_NAME . insert ( document ) 

For example

 > db . mycol . insert ({ _id : ObjectId ( 7df78ad8902c ), title : 'MongoDB Overview' , description : 'MongoDB is no sql database' , by : 'tutorials point' , url : 'http://www.tutorialspoint.com' , tags : [ 'mongodb' , 'database' , 'NoSQL' ], likes : 100 }) 

Here, mycol is the name of the Collection, created in the previous chapter. If this Collection does not already exist in the database, MongoDB will create this Collection and then insert the Document into it.

In the inserted Document, if we do not specify the _id parameter, MongoDB assigns a unique ObjectId to this Document.

_id is a unique hexadecimal number, 12 bytes long for each Document in a Collection. 12 bytes are divided as follows (described in the previous chapters):

 _id : ObjectId ( 4 bytes timestamp , 3 bytes machine id , 2 bytes process id , 3 bytes incrementer ) 

To insert multiple Documents in a single query, you can pass an array of Documents in the insert () command.

For example

 > 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 } ] } ]) 

To insert data into the Document, you can also use db.post.save (document). If you don't define _id in the Document, the save () method will work like the insert () method. If you specify _id, it will replace the entire data of the Document containing _id when specified in the save () method.

According to Tutorialspoint

Previous article: Data type in MongoDB

Next article: Query Document in MongoDB

4 ★ | 2 Vote

May be interested

  • Instructions on 2 ways to install MongoDB on Raspberry PiInstructions on 2 ways to install MongoDB on Raspberry Pi
    in this tutorial, tipsmake will guide you through the process of installing and setting up the mongodb server software on your raspberry pi.
  • How to insert an Outlook contact in WordHow to insert an Outlook contact in Word
    word supports inserting many types of files into the document so that we can access them right from the document interface, without having to open them manually. and you can completely insert contacts from outlook into word for immediate contact, or immediately provide information to document viewers.
  • Reference Database in MongoDBReference Database in MongoDB
    as shown in the relationship chapter in mongodb, to deploy a standardized database structure in mongodb, we use the referenced relationship concept, also known as manual references, in which we manipulate to store the id of the documents referenced in another document.
  • Limit records in MongoDBLimit records in MongoDB
    to limit the records in mongodb, you need to use the limit () method. the limit () method takes a parameter in a numeric format, which is the document number you want to display.
  • Advantages of MongoDBAdvantages of MongoDB
    any relation database has a unique schema design to index the data tables and relationships between those tables. meanwhile in mongodb there is no concept of relationship.
  • Data type in MongoDBData type in MongoDB
    mongodb supports many different data types. the following are some typical data types.
  • MongoDB malicious code attacks more than 26,000 victims in a weekMongoDB malicious code attacks more than 26,000 victims in a week
    malware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.
  • Covered Query in MongoDBCovered Query in MongoDB
    when all fields in the query are part of the index, mongodb connects query conditions and returns the result by using the same index without looking inside the document. when indexes are present in ram, retrieving data from indexes is faster when compared to retrieving data by scanning all documents.
  • Learn about security features and authentication in MongoDBLearn about security features and authentication in MongoDB
    in the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
  • Capped Collection in MongoDBCapped Collection in MongoDB
    capped collections are fixed-sized circular collection that follow the insert order to enhance the performance of create, read, and delete operations.