Is it possible to insert or update multiple documents with a single command in MongoDB?
I am coming fr开发者_JAVA百科om 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.
Bulk inserts will be available in a future version, currently planned for v2.1, see:
- Allow mutlidoc (bulk) insert
Most of the language drivers already implement their own bulk/batch insert too.
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
Yes. In java, this is what it looks like.
精彩评论