MongoDB insert document

Source: Internet
Author: User
Tags install mongodb mongodb tutorial

MongoDB insert document

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

All data stored in the set is in BSON format.

BSON is a type of json Binary storage format, referred to as Binary JSON.

Insert document

MongoDB uses the insert () or save () method to insert a document into the collection. The syntax is as follows:

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 ', description: 'mongodb is a Nosql database', by: 'cainiao ', url: 'http: // www.runoob.com ', tags: ['mongodb ', 'database', 'nosql'], likes: 100 })

In the above example, col is our set name. We have already created the previous chapter. If the set is not in the database, MongoDB will automatically create the set ratio and insert the document.

View the inserted document:

> Db. col. find () {"_ id": ObjectId ("56064886ade2f21f36b03134"), "title": "MongoDB tutorial", "description": "MongoDB is a Nosql Database ", "by": "cainiao tutorial", "url": "http://www.runoob.com", "tags": ["mongodb", "database", "NoSQL"], "likes ": 100}>

We can also define the data as a variable, as shown below:

 

> Document = ({title: 'mongodb ', description: 'mongodb is a Nosql database', by: 'cainiao ', url: 'http: // www.runoob.com ', tags: ['mongodb ', 'database', 'nosql'], likes: 100 });

The result is as follows:

{"Title": "MongoDB tutorial", "description": "MongoDB is a Nosql Database", "by": "cainiao tutorial", "url": "http://www.runoob.com ", "tags": ["mongodb", "database", "NoSQL"], "likes": 100}

Perform the insert operation:

> db.col.insert(document)WriteResult({ "nInserted" : 1 })>

You can also use the db. col. save (document) command to insert a document. If you do not specify the _ id field, the save () method is similar to the insert () method. If the _ id field is specified, the _ id data is updated.

For more MongoDB tutorials, see the following:

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

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.