假设我有这个数据库“list”,它包含一个名为“users”的集合,其中包含一个名为“david”的对象
{u''u id':u'david',u'url':u'url3',u'old''url':u'url3',u'wishlist':[[u'jenara',u'shards',u'nm'],[u'force of will',u'mm2',u'nm'],[u'pact of negation',u'mm',u'nm'],[u'all is dust',u'mm4',u'nm']}
如何使用pymongo编辑wishlist字段中的数组?假设我要删除四个数组中的一个,或者编辑其中一个?

最佳答案

要更新数组中的元素,请使用$set。下面是一个示例-更新第二个元素并将其值设置为["something", "else"]

db.users.update({'_id': 'david'}, {"$set": {"wishlist.1": ["something", "else"]}})

至于按索引从数组中删除项,并不是那么简单和直接,请参见:
In mongoDb, how do you remove an array element by its index
How to delete n-th element of array in mongodb

关于mongodb - pymongo:在文档中搜索和编辑字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36755740/

10-13 03:25