MongoDB Insert Document
In this section, we will introduce how to insert data into a MongoDB collection.
The document data structure is almost identical to JSON.
All data stored in the collection is in BSON format.
BSON is a binary representation of JSON-like documents, short for Binary JSON.
Insert Document
MongoDB uses the insert()
or save()
method to insert documents into a collection, with the following syntax:
db.COLLECTION_NAME.insert(document)
or
db.COLLECTION_NAME.save(document)
save()
: If the_id
primary key exists, it updates the data; if it does not exist, it inserts the data. This method is deprecated in newer versions and can be replaced withdb.collection.insertOne()
ordb.collection.replaceOne()
.insert()
: If the primary key of the data to be inserted already exists, it throws aorg.springframework.dao.DuplicateKeyException
exception, indicating a duplicate key and does not save the current data.
New methods db.collection.insertOne()
and db.collection.insertMany()
were added after version 3.2.
db.collection.insertOne()
is used to insert a single document into a collection, with the following syntax:
db.collection.insertOne(
<document>,
{
writeConcern: <document>
}
)
db.collection.insertMany()
is used to insert multiple documents into a collection, with the following syntax:
db.collection.insertMany(
[ <document 1>, <document 2>, ... ],
{
writeConcern: <document>,
ordered: <boolean>
}
)
Parameter descriptions:
document
: The document to be written.writeConcern
: The write concern, default is 1, which means the write operation requires acknowledgment, 0 means no acknowledgment is required.ordered
: Specifies whether to write in order, default istrue
, meaning write in order.
Example
The following document can be stored in the col
collection of the tutorialpro
database in MongoDB:
> db.col.insert({title: 'MongoDB Tutorial',
description: 'MongoDB is a NoSQL database',
by: 'tutorialpro.org',
url: 'http://www.tutorialpro.org',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
In the above example, col
is our collection name. If the collection does not exist in the database, MongoDB will automatically create the collection and insert the document.
To view the inserted document:
> db.col.find()
{ "_id" : ObjectId("56064886ade2f21f36b03134"), "title" : "MongoDB Tutorial", "description" : "MongoDB is a NoSQL database", "by" : "tutorialpro.org", "url" : "http://www.tutorialpro.org", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
>
We can also define the data as a variable, as shown below:
> document=({title: 'MongoDB Tutorial',
description: 'MongoDB is a NoSQL database',
by: 'tutorialpro.org',
url: 'http://www.tutorialpro.org',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
});
The result after execution is as follows:
{
"title" : "MongoDB Tutorial",
"description" : "MongoDB is a NoSQL database",
"by" : "tutorialpro.org",
"url" : "http://www.tutorialpro.org",
"tags" : [
"mongodb",
```json
{
"title" : "MongoDB",
"description" : "MongoDB is a NoSQL database",
"by" : "Runoob",
"url" : "http://www.runoob.com",
"tags" : [
"database",
"NoSQL"
],
"likes" : 100
}
Perform the insertion operation:
> db.col.insert(document)
WriteResult({ "nInserted" : 1 })
>
You can also use the db.col.save(document)
command to insert a document. If the _id
field is not specified, the save()
method is similar to the insert()
method. If the _id
field is specified, it will update the data for that _id
.