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: