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 );