0

I have two documents with "Name" values "Raja_5" and "Raja_6". I have written the following codes to update the city in two documents.

collection.update({"Name":{"$in":["Raja_5","Raja_6"]}},{"$set":{"City":"Hyd"}})

(or)

collection.update({"$or":[{"Name":"Raja_5"},{"Name":"Raja_6"}]},{"$set":{"City":"Hyd"}})

But the document with "Raja_5" is getting updated but not the other document in both the cases.

Please help me.

styvane
  • 59,869
  • 19
  • 150
  • 156

2 Answers2

1

This is because the 'multi' operator is not been set.

db.collection.update({"$or":[{"Name":"Raja_5"},{"Name":"Raja_6"}]},{"$set":{"City":"Hyd"}},  { multi: true })

above is the shell command for it.

for pymongo, i think it can be done as below

collection.update({"$or":[{"Name":"Raja_5"},{"Name":"Raja_6"}]},{"$set":{"City":"Hyd"}}, multi=True)

In version 3.0, update_many operation has been introduced with format as :

update_many(filter, update, upsert=False)

http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update_many

anish
  • 482
  • 2
  • 8
  • Thanks Anish, This is working in shell but not from pymongo. Please let me know if you know the solution – Raja Gopala Chary G Jul 15 '15 at 07:01
  • yeah, i put the shell command there :) .I have edited it now – anish Jul 15 '15 at 07:10
  • Anish, where can we get the syntax of Pymongo. – Raja Gopala Chary G Jul 15 '15 at 10:53
  • It would be like collection.update({}, {$set...}, multi=True) and you can use update({}, { "$set": ....}, upsert=False, multi=True) in cases when you want to use upsert as well. I have edited answer to put that. – anish Jul 15 '15 at 11:07
  • Thanks Anish for your help, I mean to say, is there a book/document for Pymongo Synatx – Raja Gopala Chary G Jul 15 '15 at 11:23
  • Syntax : http://api.mongodb.org/python/current/api/pymongo/collection.html Tutorial : http://api.mongodb.org/python/current/tutorial.html Online course : https://university.mongodb.com/courses/M101P/about – anish Jul 15 '15 at 11:28
1

The following code is working now

collection.update({
    "Name": {
    "$in": ["Raja_5", "Raja_6"]
    }
  }, {
    "$set": {
    "City": "Hyd"
    }
  }, "false", "true")
styvane
  • 59,869
  • 19
  • 150
  • 156