MongoDB with Python-从文档中删除特定元素



我在MongoDB-中有以下文档

_id : ObjectId("5e2f304068e7f832db1a7f55")
ListOfNames : Array
0: "John"
1: "Chris"
2: "Bob"
3: "David"
4: "Carol"

如何删除元素";约翰;在Python的帮助下,从文档中删除。

预期输出-

_id : ObjectId("5e2f304068e7f832db1a7f55")
ListOfNames : Array
0: "Chris"
1: "Bob"
2: "David"
3: "Carol"

尝试过拉,但没有成功。

在mongodb中更新文档的通用语法:

db.collection.updateOne(<filter>, <update>);

CCD_ 1和CCD_;约翰;从字段";ListOfNames;来自id为5e2f304068e7f832db1a7f55 的文档

db.collection.updateOne(
{ "_id": ObjectId("5e2f304068e7f832db1a7f55") },
{ "$pull": { "ListOfNames": "John" } }
);

问题中没有提供数据库和集合名称,假设集合名称为test,则执行相同操作的pymongo调用将为:

db.test.update_one(
{'_id': ObjectId('5e2f304068e7f832db1a7f55')},
{'$pull': {'ListOfNames': 'John'}}
)

最新更新