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 w3cschool.cn database:
>db.col.insert ({title: ' MongoDB Tutorial ', Description: ' MongoDB is a Nosql database ' by : ' W3cschool ', URL: ' http ://www.w3cschool.cn ', Tags: [' mongodb ', ' database ', ' NoSQL '], likes:100})
In the above example, Col is our collection name, the previous section we have created, and if the collection is not in that database, MongoDB will automatically create the set and insert the document.
To view a document that has been inserted:
We can also define the data as a variable, as follows:
> document= ({title: ' MongoDB Tutorial ', Description: ' MongoDB is a Nosql database ' by : ' W3cschool ', URL: '/http. www.w3cschool.cn ', Tags: [' mongodb ', ' database ', ' NoSQL '], likes:100});
After execution, the results are displayed as follows:
{ "title": "MongoDB Tutorial", "description": "MongoDB is a Nosql database", "by": "W3cschool", "url": "http://www . w3cschool.cn ", " tags ": [ " MongoDB ", " database ", " NoSQL " ], " likes ": 100}
To perform an insert operation:
Insert document You can also use the Db.col.save command. If you do not specify the _id field, the Save () method is similar to the Insert () method. If the _id field is specified, the data for the _id is updated.
MongoDB Insert Document