I am encountering difficulties with my PyMongo script: My documents have a field "used" : "false" or "true". I want to iterate over the documents whose "use" fields are "false" and use their "friends" field (which is an array of _id values) to add their own _id value to the friends field of every person who is also in the current person's friends field. It should look somewhat like this:
"person_id": "Hans",
"used": "false",
"friends": {
"Hugo",
"Kunigunde"
}
Afterwards, Hugo
and Kunigunde
should have Hans
as entry in their friends list and used
of Hans
should be true
.
My current approach is
cursor = db.People.find({"used": "false" })
for document in cursor:
But I don't know how to continue this. Thanks for helping!
For adding Hans
to Hugo
and Kunigundes
friendslist:
for friend in cursor_pointing_to_Hans['friends']:
friend_list=db.People.find({'person_id':friend})['friends']
friend_list.append(cursor_pointing_to_Hans['person_id'])
db.People.update({'person_id':friend},{'friends':friend_list})
For setting Hans
used
field to true:
db.People.update({'person_id':cursor_pointing_to_Hans['person_id']}, {'used':"true"})
Furthermore as you are working in Python I´d suggest to use the boolean python values True
and False
instead of the Strings "true" and "false"