The previous introduction to mongodb covered what mongodb is and its pros and cons in detail, and the following articles will cover the basics of mongodb usage in detail. This article begins with the mongodb insert operation.

Mongodb insert operations are divided into four types: INSERT, save, insertOne, insertMany. The following will introduce the detailed API operations and differences of the four operations in turn.

Note: If an id is not specified during the insert, MongoDB will automatically generate one for it. After successful insertion, a document is returned containing the _id of the currently successfully inserted document.

1. insert

Description:

Inserts single or multiple documents into a collection

Grammar:

db.collection.insert(

.

{

writeConcern: ,

ordered:

}

)

Parameter explanation:

Document: The document to be inserted, mandatory parameter

WriteConcern: This parameter is optional. Files expressing write concerns ignore the default write concerns. This parameter will be demonstrated in more detail in the advanced mongodb tutorial, Replica Clusters.

Ordered: This parameter is optional. A Boolean value that specifies whether the Mongod instance should perform an ordered or unordered insert. By default, true, executing an ordered list of operations on a sharded collection is usually slower than executing an unordered list because each operation must wait for the previous one to complete

1.1. The sample

db.insertExample.insert(

[

{ _id: 11, item: “pencil”, qty: 50, type: “no.2” },

{ item: “pen”, qty: 20 },

{ item: “eraser”, qty: 25 }

]

)

Return result:

{ acknowledged: 1,

insertedIds:

{‘ 0 ‘: 11.

‘1’ :

{ _bsontype: ‘ObjectID’,

id: ObjectId(“5fbb92ad4906484955bb1bfa”)

‘2’ :

{ _bsontype: ‘ObjectID’,

id: ObjectId(“5fbb92ad4906484955bb1bfb”)

}

2. save

Description:

Update an existing document or insert a new document, depending on the document parameters. The save() method uses insert or update commands, and if the document does not contain the _ID field, the save() method calls the insert() method. During the operation, mongo shell will create an ObjectId and assign it to the _ID field. If the document contains the _ID field, then the save() method is equivalent to the update of the query document on the _id field with the upsert option set to true.

Grammar:

db.collection.save(

.

{

writeConcern:

}

)

Parameter explanation:

Document: The document to be saved in the collection.

WriteConcern: Optional. Documents that express authoring concerns. Omit to use the default write concern,

If running in a transaction, do not explicitly set the write concern for the operation. To use write attention for transactions, see Read Attention/Write attention/Read Preferences, which will be demonstrated in more detail later in advanced Mongodb – Replica Clusters.

2.1. The sample

db.saveExample.save( { item: “book”, qty: 40 } )

Return result:

{ “_id” : ObjectId(“5fe3082595b782487444fa0f”), “item” : “book”, “qty” : 40 }

Update qTY to 30 and item to water

db.saveExample.save( { _id: ObjectId(“5fe3082595b782487444fa0f”), item: “water”, qty: 30 } )

Return result:

{ “_id” : ObjectId(“5fe3082595b782487444fa0f”), “item” : “water”, “qty” : 30 }

3. insertOne

Description:

Inserts a single document into the collection

Grammar:

db.collection.insertOne(

{},

{

writeConcern:

})

Parameter explanation:

Document: The document to be inserted, mandatory parameter

WriteConcern: This parameter is optional. Files expressing write concerns ignore the default write concerns. This parameter will be demonstrated in more detail in the advanced mongodb tutorial, Replica Clusters.

3.1. The sample

db.insertOneExample.insertOne(

{ “item”: “envelopes”, “qty”: 100, type: “Self-Sealing” },

{writeConcern: {w: “majority”, wtimeout: 100}}// Given a replica set of three members, the following operation specifies that w is the majority and wtimeout is 100 milliseconds:

);

4. insertMany

Description:

Insert multiple documents into collections [The operand number in each group cannot exceed the value of the maxWriteBatchSize database. Starting with MongoDB 3.6, this value is 100,000. The value shown in the isMaster maxWriteBatchSize field, value view maxWriteBatchSize command: db. RunCommand ({” isMaster “: 1}).

Grammar:

db.collection.insertMany(

[ <document 1> , <document 2>, … ] .

{

writeConcern: ,

ordered:

}

)

Parameter explanation:

Document: The document to be inserted, mandatory parameter

WriteConcern: This parameter is optional. Files expressing write concerns ignore the default write concerns. This parameter will be demonstrated in more detail in the advanced mongodb tutorial, Replica Clusters.

Ordered: This parameter is optional. A Boolean value that specifies whether the Mongod instance should perform an ordered or unordered insert. The default is true. Executing an ordered list of operations on a shard collection is usually slower than executing an unordered list, because with an ordered list each operation must wait for the previous one to complete, and when an error is encountered during sequential writes, the operation stops and the rest of the document is not written, correct or not

4.1. The sample

db.insertManyExample.insertMany( [

{ _id: 10, item: “large box”, qty: 20 },

{ _id: 11, item: “medium box”, qty: 30 },

{ _id: 12, item: “envelope”, qty: 100},

{ _id: 13, item: “tape”, qty: 20},

{ _id: 14, item: “bubble wrap”, qty: 30}

], { ordered: false} );

Next update mongodb basic operations