简体   繁体   English

在mongodb JavaScript控制台中从Array批量插入

[英]Bulk insert from Array in mongodb JavaScript console

I'm trying to make a bulk insert from the MongoDB console of an array into a collection. 我正在尝试从数组的MongoDB控制台批量插入到集合中。

I'd like to do something similar to this. 我想做类似的事情。

obj1 = {_id:ObjectId(),blabla:1}; obj1 = {_ id:ObjectId(),blabla:1};

obj2 = {_id:ObjectId(),blabla:2}; obj2 = {_ id:ObjectId(),blabla:2};

objs = [obj1, obj2]; objs = [obj1,obj2];

db.test.insert(objs); db.test.insert(OBJ文件);

db.test.find() db.test.find()

> {"_id": ObjectId("xxxx"), "blabla": 1} > {"_id": ObjectId("xxxx"), "blabla": 2} > {“_ id”:ObjectId(“xxxx”),“blabla”:1}> {“_ id”:ObjectId(“xxxx”),“blabla”:2}

But, instead of inserting two objects on the collection, it stores one list with the two objects. 但是,它不是在集合上插入两个对象,而是存储一个包含两个对象的列表。

db.test.find() db.test.find()

> {"_id": ObjectId("xxx"), "0":{"_id": ObjectId("xxxx"), "blabla": 1}, "1":{"_id": ObjectId("xxxx"), "blabla": 2} } > {“_ id”:ObjectId(“xxx”),“0”:{“_ id”:ObjectId(“xxxx”),“blabla”:1},“1”:{“_ id”:ObjectId(“xxxx” ),“blabla”:2}}

That functionality appears to present on other drivers (like pymongo ), but I can't find a way of doing that from the mongodb console, in JavaScript code. 该功能似乎出现在其他驱动程序(如pymongo )上,但我无法通过JavaScript代码从mongodb控制台找到这样做的方法。

The feature to insert multiple documents into a collection has been added to Mongo command shell version 2.1+. Mongo命令shell版本2.1+中添加了将多个文档插入集合的功能。 Now You can insert and array of documents into your collection. 现在您可以将文档插入到您的集合中。

Example: 例:

db.users.insert([{name:'Jon', email:'Jon_Doe@mail.com'},{name:'Jane', email:'Jane_Doe@mail.com'}])

For more information look at these jira closed feature request: 有关更多信息,请查看这些jira已关闭功能请求:

https://jira.mongodb.org/browse/SERVER-3819 https://jira.mongodb.org/browse/SERVER-3819

https://jira.mongodb.org/browse/SERVER-2395 https://jira.mongodb.org/browse/SERVER-2395

There is an existing feature request for this. 现有功能请求。 http://jira.mongodb.org/browse/SERVER-2429 http://jira.mongodb.org/browse/SERVER-2429

Bulk is available in 2.6.6. 批量发货在2.6.6。

var bulk=db.posts.initializeUnorderedBulkOp() ;
bulk.insert(obj1) ; bulk.insert(obj2);...

bulk.execute() ;
objs.forEach(function(obj) { db.test.insert(obj) });

You cannot do a bulk insert through the interactive command shell. 您无法通过交互式命令shell执行批量插入。 But there is a commandline tool that ships with Mongo that allows you to import multiple records as either JSON (your best option), CSV or Binary. 但Mongo附带了一个命令行工具,允许您将多个记录导入为JSON(最佳选项),CSV或二进制。

> mongoimport -h <host> -d <database> -c <collection> -u <user> -p <password> --file <input file> --jsonArray

The jsonArray flag will allow you to insert multiple records when you use the json array file. 使用json数组文件时, jsonArray标志将允许您插入多个记录。 Another important note is to make sure you have the file ending with a new line character. 另一个重要的注意事项是确保文件以新行字符结尾。 Otherwise the last line will not be parsed. 否则,不会解析最后一行。

简而言之:mongo控制台级别没有这样的批量插入API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM