Latest web development tutorials

MongoDB into the document

This chapter we will show you how to insert data into the collection MongoDB.

JSON data structure of the document and essentially the same.

All data is stored in the collection are BSON format.

BSON is a binary form of a class json storage format called Binary JSON.

Insert Document

MongoDB using the insert () or save () method to insert a document to the collection, the syntax is as follows:



The following documents can be stored in the w3big col collection MongoDB database:

>db.col.insert({title: 'MongoDB 教程', 
    description: 'MongoDB 是一个 Nosql 数据库',
    by: '本教程',
    url: '',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100

Col above example is our collection name, the previous section we have already created, and if the set is not in the database, MongoDB will automatically create the collection and insert documents.

View into the document:

> db.col.find()
{ "_id" : ObjectId("56064886ade2f21f36b03134"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "本教程", "url" : "", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }

We can also be defined as a data variable as follows:

> document=({title: 'MongoDB 教程', 
    description: 'MongoDB 是一个 Nosql 数据库',
    by: '本教程',
    url: '',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100

After the implementation of the results are as follows:

        "title" : "MongoDB 教程",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "本教程",
        "url" : "",
        "tags" : [
        "likes" : 100

Insert operation:

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

Into a document You can also use (document) command. If you do not specify _id field save () method is similar to insert () method. If you specify _id field, the _id data is updated.