MongoDB Tutorial Lesson Sixth MongoDB Insert Document

Source: Internet
Author: User
Tags mongodb collection mongodb tutorial

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

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.