简体   繁体   中英

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. It does not seem like there is an option like that in MongoDB. Is that correct?

I realize that is not exactly compatible with MongoDB object-based approach. 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:

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.

This might be of use:

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:

Most of the language drivers already implement their own bulk/batch insert too.

Yes. In java , this is what it looks like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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