Updating multiple documents

936 views
Skip to first unread message

Abhishek kumar

unread,
Oct 20, 2011, 2:09:52 AM10/20/11
to mongodb-user
I have to update one of my collection, which is multi document update

Let collection "A"
A: has fields
{"_id", "field1", "field2", "field3"} // I needed to add
field3 in below update() if its not present or just update if its
present

basciDBObject.put("field1","field_value")

updateDate.put("field3","filed_value")

What I did
collection.update(basicDBObject,updateDate,false,true);

On running the command mongo is telling:
multi update only works with $ operators

Kindly help, what am I missing

Karl Seguin

unread,
Oct 20, 2011, 2:24:20 AM10/20/11
to mongod...@googlegroups.com
multiple updates only work with the $ modifiers: http://www.mongodb.org/display/DOCS/Updating#Updating-ModifierOperations

This is probably what you want:
BasicDBObject updateDate = new BasicDBObject("$set", new BasicDBObject("field3", fieldValue));

If you did what you are trying, without a multi update, it would replace the entire document with {field3: fieldValue}...unlike SQL, updates in MongoDB don't work on individuals fields by default..it works on the entire document (which makes sense when you consider fields are "dynamic").

Karl

Abhishek kumar

unread,
Oct 20, 2011, 3:35:42 AM10/20/11
to mongodb-user
thanks..
Reply all
Reply to author
Forward
0 new messages