In this section, we will show you how to insert data into a collection of MongoDB.
The data structure of the document is basically the same as that of JSON.
All data stored in the collection is in BSON format.
BSON is a binary storage format similar to JSON, which is short for Binary JSON. MongoDB usage Added after version 3.2 Parameter description: Document: the document to write to. WriteConcern: write policy, default is 1, that is, confirm write operation is required, 0 is not required. Ordered: specifies whether to write sequentially, default The following documents can be stored in the col collection of MongoDB’s runoob database: In the above example View the inserted document: We can also define the data as a variable, as follows: After execution, the results are displayed as follows: Perform the insert operation: You can also insert a document using the 3.13.1. Insert document ¶
insert()
Or
save()
Method to insert a document into the collection with the following syntax:db.COLLECTION_NAME.insert(document)
或
db.COLLECTION_NAME.save(document)
save()
Update the data if the_ id primary key exists, and insert the data if it does not exist. This method is obsolete in the new version and can be used db.collection.insertOne() Or db.collection.replaceOne() To replace it.
insert()
Will be thrown if the inserted data primary key already exists org.springframework.dao.DuplicateKeyException Exception, prompting that the primary key is repeated and the current data is not saved.
db.collection.insertOne()
And
db.collection.insertMany()
.
db.collection.insertOne()
Used to insert a new document into the collection in the following syntax format:db.collection.insertOne(
<document>,
{
writeConcern: <document>
}
)
db.collection.insertMany()
Used to insert multiple documents into the collection in the following syntax format:db.collection.insertMany(
[ <document 1> , <document 2>, ... ],
{
writeConcern: <document>,
ordered: <boolean>
}
)
true
To write sequentiallyExample ¶
>db.col.insert({title: 'MongoDB 教程',
description: 'MongoDB 是一个 Nosql 数据库',
by: '菜鸟教程',
url: 'http://www.runoob.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
col
Is the name of our collection, and if the collection is not in the database, MongoDB automatically creates the collection and inserts the document.> db.col.find()
{ "_id" : ObjectId("56064886ade2f21f36b03134"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
>
> document=({title: 'MongoDB 教程',
description: 'MongoDB 是一个 Nosql 数据库',
by: '菜鸟教程',
url: 'http://www.runoob.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
});
{
"title" : "MongoDB 教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "菜鸟教程",
"url" : "http://www.runoob.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
> db.col.insert(document)
WriteResult({ "nInserted" : 1 })
>
db.col.save(document)
Orders. If you do not specify the_ id field
save()
The method is similar to
insert()
Method. If you specify the_ id field, the data for that_ id is updated.