1

提到这个问题,现在我想从中删除dinnerRandom Name 2我该怎么做:这是我尝试过的:

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'}

4

1 回答 1

2

演示 - 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 } } }
})
于 2021-04-14T05:59:38.190 回答