Insert Document in MongoDB
To insert data into Collection in MongoDB, you need to use the insert () or save () method.
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
You should read it
- How to insert an Outlook contact in Word
- Delete Document in MongoDB
- Query Document in MongoDB
- Index (Mong) in MongoDB
- Capped Collection in MongoDB
- Projection in MongoDB
- How to Insert Hyperlinks in Microsoft Word
- How to Insert a Multiple Page PDF Into a Word Document
- How to insert and edit SVG images in Office 2016?
- Overview of MongoDB
- Update Document in MongoDB
- How to Insert a Hyperlink in Microsoft Word
Maybe you are interested
A mask asteroid flies across the Earth About 6 billion years from now, the light from the dying Sun will burn the asteroid belt The MIT team claims to find the most effective way of 'intercepting' meteorites Choose headphones like in-ear, on-ear or over-ear? Which Sony MDR- ZX310 and Sennheiser HD 201 headphones are better? The precious travel experience for those who first arrive in Oakland, California