MongoDB does not support batch insertion.

Source: Internet
Author: User
The structure of mongodb is different from that of a relational database. It is similar to the tree structure and can be easily operated on each branch. However, it does not insert (value, value, value...) Like mysql ...) this syntax does not support transaction. Therefore, mongodb cannot insert data in batches. If you want to insert data in batches, you can write a loop in mongodb,

The structure of mongodb is different from that of a relational database. It is similar to the tree structure and can be easily operated on each branch. However, it does not insert (value, value, value...) Like mysql ...) this syntax does not support transaction. Therefore, mongodb cannot insert data in batches. If you want to insert data in batches, you can write a loop in mongodb,

The structure of mongodb is different from that of a relational database. It is similar to the tree structure and can be easily operated on each branch. However, it does not insert (value, value, value...) Like mysql ...) such a syntax does not support transaction.
Therefore, mongodb cannot insert data in batches. If you want to insert data in batches, you can write a loop in mongodb, which supports syntax such as.


Because the underlying MongoDB does not support this operation.
From the oplog mode of MongoDB, we can see that each op of MongoDB is the smallest unit for independent execution. Therefore, there is no combination of multiple op into one execution. Therefore, this batch insert operation does not exist in various APIs.

A netizen mentioned his understanding and found it very reasonable:
This involves the essential differences between traditional relational databases and NoSQL, which are frequently used. Each NoSQL operation is lightweight and small, with no additional operations except Data Writing.
Analogy: MongoDB simply throws things into the corresponding Cabinet (database) when putting things (data), while MySQL needs to maintain communication with the deliverer (two-way connection ), storage of things in different la S (with mode ).
The batch insert of MySQL reduces the communication and sharding processes, and MongoDB itself does not have these processes. Therefore, MongoDB does not have the concept of batch insert.

I used to test the code for batch data insertion into mongodb using php:

$ Conn = new Mongo ();


$ Db = $ conn-> selectDB ("test"); // set the database name here

$ Collection = $ db-> mytable; // select a table
/*
+ ----------------------------------------------------------------
Insert data in batches and insert 0.1 million data records at a time. If you change the number, you can insert more data.
+ ----------------------------------------------------------------
*/
$ Users = array ();
For ($ I = 0; I I <100000; $ I ++ ){
$ Users [] = array ('username' => 'user'. $ I, 'I' => $ I );
}

$ OK = $ collection-> batchInsert ($ users );

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.