简体   繁体   English

是否可以在MongoDB中使用单个命令插入或更新多个文档?

[英]Is it possible to insert or update multiple documents with a single command in MongoDB?

I am coming from MySQL where you can have a huge INSERT and add (or ON DUPLICATE KEY UPDATE) a bunch of rows in a single query rather than having a loop with separate queries for each row. 我来自MySQL,在那里你可以有一个巨大的INSERT并在一个查询中添加(或者在DUPLPLATE KEY UPDATE)一堆行,而不是为每一行都有一个单独查询的循环。 It does not seem like there is an option like that in MongoDB. 在MongoDB中似乎没有像这样的选项。 Is that correct? 那是对的吗?

I realize that is not exactly compatible with MongoDB object-based approach. 我意识到这与MongoDB基于对象的方法并不完全兼容。 It just seems somewhat inefficient to send thousands of commands when one will do, especially if the DB is on a separate server. 发送成千上万的命令似乎有点低效,特别是如果数据库位于单独的服务器上。

Yes, the low-level interface supports things like this: 是的,低级接口支持这样的事情:

db.nonsense.update({a: 'a'}, {$set: {b: 'X'}}, false, true);

And that's like this SQL: 这就像这个SQL:

update nonsense
set b = 'X'
where a = 'a'

You'll want to last parameter ( multi ) to be true or you won't update all the matching entries. 您希望最后一个参数( 多个 )为true或者您不会更新所有匹配的条目。

This might be of use: 这可能有用:

http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

Bulk inserts will be available in a future version, currently planned for v2.1, see: 批量插入将在未来版本中提供,目前计划用于v2.1,请参阅:

Most of the language drivers already implement their own bulk/batch insert too. 大多数语言驱动程序也已经实现了自己的批量/批量插入。

Yes. 是。 In java , this is what it looks like. java中 ,这就是它的样子。

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

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