简体   繁体   English

是否可以通过单个insert()调用将多个文档插入MongoDB集合?

[英]Is it possible to insert several documents to a MongoDB-collection with a single insert() call?

I defined 2 variables in the interactive shell of MongoDB: 我在MongoDB的交互式shell中定义了2个变量:

a = {"letter": "a"}
b = {"letter": "b"}

Now I'd like to add them to a collection newColl in the database stackoverflow : 现在我想将它们添加到数据库stackoverflow的集合newColl

use stackoverflow
db.newColl.insert(a)
db.newColl.insert(b)

I was wondering, if it is possible to insert both documents in one go, for example like: 我想知道,如果可以一次插入两个文件,例如:

// Not working!
db.newColl.insert(a, b)

如果将两个文档放入数组中,它将起作用:

db.newColl.insert([a, b])

Just use arrays: 只需使用数组:

db.collection.isert([doc1:adsadad, doc2:asdad, etc, etc])

http://docs.mongodb.org/manual/reference/method/db.collection.insert/ http://docs.mongodb.org/manual/reference/method/db.collection.insert/

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

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