MongoDB Insert Document

Source: Internet
Author: User
Tags mongodb tutorial

MongodbInsert Document

In this section we will show you how to insert data into a collection of MongoDB.

The data structure of the document is basically the same as JSON.

All data stored in the collection is in the Bson format.

Bson is a binary form of a class JSON storage format, referred to as binary JSON.

Insert Document

MongoDB uses the Insert () or Save () method to insert a document into the collection, with the following syntax:

DB.  Collection_name.  Insert(document)      
Instance

The following documents can be stored in the Col collection of the MongoDB Runoob database:

>Db.Col.Insert({Title: ' MongoDB Tutorial ', Description:  MongoDB is a Nosql database ' , by:  Rookie tutorial ' , Tags: [  ' MongoDB ' ,  ' database '  ,   "NoSQL" ], Likes:< Span class= "PLN" > 100})       

In the above example Col is our collection name, the previous section we have created, if the collection is not in that database, MongoDB will automatically create the collection than insert the document.

To view a document that has been inserted:

>Db.Col.Find(){ "_ID" : ObjectId("56064886ade2f21f36b03134"), "Title" : "MongoDB Tutorial", "description" : "MongoDB is a Nosql database", "By" :  "rookie Tutorial" ,  "url"  :  "http://www.runoob.com" , span class= "str" > "tags"  : [ "MongoDB" ,  "database"  ,   "NoSQL"  ],  "Likes"  : 100 }>          

We can also define the data as a variable, as follows:

>Document=({Title: ' MongoDB Tutorial ', Description:  MongoDB is a Nosql database ' , by:  Rookie tutorial ' , Tags: [  ' MongoDB ' ,  ' database '  ,   "NoSQL" ], Likes:< Span class= "PLN" > 100       

After execution, the results are displayed as follows:

{        "Title" : "MongoDB Tutorial", "description" : "MongoDB is a Nosql database", "By" :  "rookie Tutorial" ,  "url"  :  "http://www.runoob.com" ,  "tags"  :[  "MongoDB" ,  "database" ,  "NoSQL"  ],  "likes"  :  100}        

To perform an insert operation:

> db.  Col.  Insert(document)Writeresult({"ninserted":1})>  

MongoDB Insert Document

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.