- Write operation--add operation
MongoDB provides the following actions to perform the Add document operation
- Db.collection.insertOne () 3.2 New additions
- Db.collection.insertMany () 3.2 New additions
- Db.collection.insert ()
- First introduce the next Insertone () operation
Syntax rules:
Db.collection.insertOne ( <document>, { <document> // Optional. A document expressing the write concern. Omit to use the default write concern. })
About Writeconcern the sections that followed are detailed
return value:
A document containing:
- A Boolean acknowledged as true if the operation ran with write concern or false if WRI Te concern was disabled.
- A single element Insertedid with the _id of the inserted document.
Attention:
The operation creates a collection if the collection does not exist.
This operation does not support Db.collection.explain () query plans can use Insert instead.
Instance:
Try { "card", Qty:15 }) ; Catch (e) { print (e);};
return value:
{ true, "Insertedids" : [ ObjectId (" 562a94d381cb9f1cd6eb0e1a "), ]}
Copy set after adding the write concern option
Try { db.products.insertOne ( "item": "Envelopes", "Qty": +, type: "Self-sealing" }, " Majority ", Wtimeout:100 }}; W:majority, which indicates that the >1/2 node has data )}catch (e) { print (e);}
If a time-out returns a result instance
writeconcernerror ({ "code": +, "Errinfo" : { true }, " ErrMsg ":" Waiting for replication timed out "})
- Introduction to the next Insermany () operation is a newly added feature of version 3.2
Syntax rules:
Db.collection.insertMany ( <document 1>, <document 2>, ...]},//an array of documents to INS ERT into the collection. Note is the array {<document>, <boolean> mongod True. The default order added in order is slower than adding it in order )
return value:
A document containing:
- A Boolean acknowledged as true if the operation ran with write concern or false if WRI Te concern was disabled
- An array of _id to each successfully inserted documents
Attention:
- Each set of operations can have up to 1000 documents.
- The order is added at a slower rate than is added in order.
- Collections that do not exist are created automatically.
- Insertmany () also does not support Db.collection.explain () can use Insert instead .
- If an error is added, the bulkwriteerror exception exception is reported, and an operation that is added sequentially encounters an error that stops directly, but does not sequentially continue the write operation in the queue.
Example: Do not specify _id
Try { Db.products.insertMany ([ "card", Qty:15 }, "envelope", qty:20 }, " Stamps ", qty:30 } ]);} Catch (e) { print (e);}
return value:
{ true, "Insertedids" : [ ObjectId (" 562a94d381cb9f1cd6eb0e1a "), ObjectId (" 562a94d381cb9f1cd6eb0e1b "), ObjectId (" 562a94d381cb9f1cd6eb0e1c ") ]}
Specify the _id field:
Try { Db.products.insertMany ([ ten], item: "Large box", Qty:20 } ,One, item: "Small box", Qty:5 5 }, item: "Medium box", Qty:30 } ]);} Catch (e) { print (e);}
return value:
true, "Insertedids": [10, 11, 12]}
Insert a repeating field for the _id field:
Try { db.products.insertMany ("Item:" Envelopes ", qty:60}, and item:" Stamps ", qty:110 }, +, item: "Packing Tape", qty:38 } ]);} Catch (e) { print (e);}
Return value: The return Bulkwriteerror exception will only add the first piece of data
Bulkwriteerror ({"Writeerrors" : [ { "Index": 0, "Code": 11000, "ErrMsg": "E11000 duplicate key error collection:restaurant.test index: _id_ dup key: {: 13.0}", "Op" : { "_id": 13, "Item": "Envelopes", "Qty": 60 } } ], "Writeconcernerrors" : [ ], "Ninserted": 0, "Nupserted": 0, "Nmatched": 0, "Nmodified": 0, "Nremoved": 0, "Upserted" : [ ]})
There is no sequential add operation:
Try { Db.products.insertMany ([ ten], item: "Large box", Qty:20 } ,One, item: "Small box", Qty:5 5 }, One, item: "Medium box", Qty:30 }, page, item: "Envelope", qty:100}, and ite M: "Stamps", qty:125 }, $, item: "Tape", Qty:20}, +, item: "Bubble wrap", qty:30 } false }); Catch (e) { print (e);}
Return value: The following field can be added correctly even if an error occurs
Bulkwriteerror ({"Writeerrors" : [ { "Index": 2, "Code": 11000, "ErrMsg": "E11000 duplicate key error collection:inventory.products index: _id_ dup key: {: 11.0}", "Op" : { "_id": 11, "Item": "Medium Box", "Qty": 30 } }, { "Index": 5, "Code": 11000, "ErrMsg": "E11000 duplicate key error collection:inventory.products index: _id_ dup key: {: 13.0}", "Op" : { "_id": 13, "Item": "Tape", "Qty": 20 } } ], "Writeconcernerrors" : [ ], "Ninserted": 5, "Nupserted": 0, "Nmatched": 0, "Nmodified": 0, "Nremoved": 0, "Upserted" : [ ]})
With write security level Writeconcern and Wtimeout
Try { db.products.insertMany ( [ ten], item: "Large box", Qty:20 } ,One, item: "Small box", q Ty:55 }, page, item: "Medium box", Qty:30 } ], "majority", wtimeout:100 }
);}
Catch (e) { print (e);}
Return value: If The primary and at least one secondary acknowledge each write operation within
{ true, "Insertedids" : [ ObjectId ("562a94d381cb9f1cd6eb0e1a") ), ObjectId ("562a94d381cb9f1cd6eb0e1b"), ObjectId ("562a94d381cb9f1cd6eb0e1c") ]}
Timeout returned:
- Writeconcernerror ({ "code": +, "Errinfo" : { "wtimeout": true }, "errmsg": "Waiting for replication timed out" })
- Introduction to the next Inser () operation
Grammar:
Db.collection.insert ( <document or array of documents>, //arrays or documents can be added { <document>, <boolean>//Follow Insertmany () Action })
return value:
- A writeresult object for single inserts.
- A bulkwriteresult object for bulk inserts.
mongodb3.2 System Learning--1, document insert Insertone Insertmany