Doc*_*tor 1 python arrays mongodb pymongo
I have this simple JSON in mongodb:
{
"pwd_list": [
{
"pwd": "password1",
"pwd_date": str(int(time.time()))
},
{
"pwd": "password2",
"pwd_date": str(int(time.time()))
},
]
}
Run Code Online (Sandbox Code Playgroud)
What I am simply trying to do is to update one of the row of the pwd_list array using an index...
I tried to use the $position of mongodb but it seems to only work with $push... but I don't want to push !
(I'm using pymongo)
So I tried different things like this one :
self.collection.update({"email": "email"},
{
"$set": {
"pwd_list": {
"temp_passwd": "NEW VALUE",
"temp_date": "str(int(time.time()))"
}
}
})
Run Code Online (Sandbox Code Playgroud)
But it it not working as expected. (The above example is transforming the array in object...)
If it is not possible, can I at least update the first row (always this one) ?
Thanks in advanced !
执行此操作时,您有两种选择。如果要搜索要更改的特定项目,则应执行以下操作:
self.collection.update({"email": "email", "pwd_list.pwd": "Password2"},
{
"$set": {
"pwd_list.$": {
"pwd": "NEW VALUE",
"pwd_date": "str(int(time.time()))"
}
}
})
Run Code Online (Sandbox Code Playgroud)
如果您想更改特定项目,并且您知道该项目在数组中的位置,您应该执行以下操作(在我的示例中,我正在更改第一个项目):
self.collection.update({"email": "email"},
{
"$set": {
"pwd_list.0": {
"pwd": "NEW VALUE",
"pwd_date": "str(int(time.time()))"
}
}
})
Run Code Online (Sandbox Code Playgroud)
您可以在此页面上找到更多详细信息。
| 归档时间: |
|
| 查看次数: |
1340 次 |
| 最近记录: |