[英]PyMongo cursor - Modify and use value
I am encountering difficulties with my PyMongo script: My documents have a field "used" : "false" or "true". 我的PyMongo脚本遇到困难:我的文档有一个字段“ used”:“ false”或“ 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.
我想遍历其“使用”字段为“假”的文档,并使用其“朋友”字段(由_id值组成的数组)将自己的_id值添加到同样位于“当前人的朋友字段。 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
. 此后,
Hugo
和Kunigunde
应该有Hans
在他们的朋友列表条目和used
的Hans
应该是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: 要将
Hans
添加到Hugo
和Kunigundes
朋友列表中:
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: 要将
Hans
used
字段设置为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" 此外,当您使用Python时,我建议使用布尔python值
True
和False
而不是字符串“ true”和“ false”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.