MongoDB Operations Manual crud Insertion

Source: Internet
Author: User
Tags bulk insert

Insert Operation inserting records1. Insert a record Db.testData.insert ({num:1,name: ' a '}); result Writeresult ({"ninserted": 1})
2. View the inserted record db.testData.find ();
Insert Array 1. Define an array var arr = [{ Num:1,name: ' A '},{num:2,name: ' B '},{num:3,name: ' C '}];
2. Insert record db.testdata.insert (arr);
results ninserted display several data were inserted
Bulkwriteresult ({
" writeerrors ": [],
"writeconcernerrors": [],
"ninserted": 3,
"nupserted": 0,
"nmatched": 0,
"nmodified": 0,
"nremoved": 0,
"upserted": []
})
Bulk INSERT multiple Data Bulk is a new feature of 2.6
1. Initialize the batch operation constructor for the specified collectionvar bulk = Db.testData.initializeUnorderedBulkOp ();Initializing an unordered batch operation
This operation returns an unordered operation constructor that maintains the list of actions. The unsorted meaning is that MongoDB can synchronously perform multiple operations without order.
If an error occurs during the period, subsequent operations continue.
You can also construct an orderly operation constructor to view the Db.collection.initializeOrderedBulkOp ();
2. Add the insert operation to the bulk object
Bulk.insert ({num:4,name: ' d '});
Bulk.insert ({num:5,name: ' e '});
3. Performing batch Processing
Bulk.execute ();
Returns the result of the batch, ninserted represents a few inserts, and if an error occurs during the period, it is included in the result.
Bulkwriteresult ({
"Writeerrors": [],
"Writeconcernerrors": [],
"Ninserted": 2,
"nupserted": 0,
"nmatched": 0,
"Nmodified": 0,
"Nremoved": 0,
"upserted": []
})
Additional examples and methods db.collection.update () method,
Db.collection.findAndModify (),
Db.collection.save ()
These methods can also perform insert operations.

MongoDB Operations Manual crud Insertion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.