如何提取嵌套文档
How to pull a nested Document
参考这个 ,现在我想从 Random Name 2
中删除 dinner
,我该怎么做:
这是我尝试过的:
din_val = data[0]
condition = {
"username" : session['user']["username"],
f"likings.{din_val}" : {
"$exists" : True
}
}
update_value = {
"$pull" : {
f"likings.$" : din_val
}
}
response = mongo.db.appname.update(condition, update_value)
但我收到的错误是
pymongo.errors.WriteError: Cannot apply $pull to a non-array value, full error: {'index': 0, 'code': 2, 'errmsg': 'Cannot apply $pull to a non-array value'}
演示 - https://mongoplayground.net/p/uYexli31rsc
https://docs.mongodb.com/manual/reference/operator/update/pull/
db.collection.update(
{ "name": "Random Name 2" },
{ $pull: { "likings": { dinner: { $exists: true } } }
})
参考这个 Random Name 2
中删除 dinner
,我该怎么做:
这是我尝试过的:
din_val = data[0]
condition = {
"username" : session['user']["username"],
f"likings.{din_val}" : {
"$exists" : True
}
}
update_value = {
"$pull" : {
f"likings.$" : din_val
}
}
response = mongo.db.appname.update(condition, update_value)
但我收到的错误是
pymongo.errors.WriteError: Cannot apply $pull to a non-array value, full error: {'index': 0, 'code': 2, 'errmsg': 'Cannot apply $pull to a non-array value'}
演示 - https://mongoplayground.net/p/uYexli31rsc
https://docs.mongodb.com/manual/reference/operator/update/pull/
db.collection.update(
{ "name": "Random Name 2" },
{ $pull: { "likings": { dinner: { $exists: true } } }
})