如何删除 pymongo 数组中的子字典?
How can I remove a subdict in array in pymongo?
我目前有一个 MongoDB 集合,如下所示:
{
"_id": {
"$numberLong": "1234567890"
},
"playlists": [{
"name": "ff",
"author": {
"$numberLong": "123456789"
},
"tracks": [{
"name": "name1",
"uri": "uri1",
"duration": 100
}, {
"name": "name2",
"uri": "uri2",
"duration": 120
}, ...
]
}], ...
}
如何删除包含 {"name": "name2"}
的子字典?
我试过了
colletion.update_one({"_id" : 1234567890}, {"$unset" : {{"playlists" : {{{"name" : playlist}: {f"tracks.{str(track)}"} : 1}}}}})
但是没用。
非常感谢!
您需要 $pull
操作员。要删除播放列表文件 ({ "name": "ff" }
) 下带有 { "name": "name2" }
的曲目文件,您需要应用 $[<identifier>]
过滤位置运算符和 arrayFilters
.
db.collection.update({
"_id": 1234567890
},
{
"$pull": {
"playlists.$[playlist].tracks": {
"name": "name2"
}
}
},
{
arrayFilters: [
{
"playlist.name": "ff"
}
]
})
Pymongo
colletion.update_one({
"_id": 1234567890
},
{
"$pull": {
"playlists.$[playlist].tracks": {
"name": "name2"
}
}
},
array_filters = [
{
"playlist.name": "ff"
}
])
我目前有一个 MongoDB 集合,如下所示:
{
"_id": {
"$numberLong": "1234567890"
},
"playlists": [{
"name": "ff",
"author": {
"$numberLong": "123456789"
},
"tracks": [{
"name": "name1",
"uri": "uri1",
"duration": 100
}, {
"name": "name2",
"uri": "uri2",
"duration": 120
}, ...
]
}], ...
}
如何删除包含 {"name": "name2"}
的子字典?
我试过了
colletion.update_one({"_id" : 1234567890}, {"$unset" : {{"playlists" : {{{"name" : playlist}: {f"tracks.{str(track)}"} : 1}}}}})
但是没用。 非常感谢!
您需要 $pull
操作员。要删除播放列表文件 ({ "name": "ff" }
) 下带有 { "name": "name2" }
的曲目文件,您需要应用 $[<identifier>]
过滤位置运算符和 arrayFilters
.
db.collection.update({
"_id": 1234567890
},
{
"$pull": {
"playlists.$[playlist].tracks": {
"name": "name2"
}
}
},
{
arrayFilters: [
{
"playlist.name": "ff"
}
]
})
Pymongo
colletion.update_one({
"_id": 1234567890
},
{
"$pull": {
"playlists.$[playlist].tracks": {
"name": "name2"
}
}
},
array_filters = [
{
"playlist.name": "ff"
}
])