重命名 find() 中的 _id 字段
rename _id field in find()
我使用 file_name 作为集合中的 _id。
但我不想查询 _id 并将其更改为 file_name everytime.Is 有什么方法可以重命名 python 中的名称?
我知道的方法是
data = mongo.collection.find()
for row in data:
row['file_name'] = row.pop('_id')
或
mongo.collection.aggregate({},{'$project':...})
哪个更好。或者其他更好的方法。
中找到了这个
来自mongodb4.4+
使用mongo.collection.find({},{'filename':'$_id','_id':0})
我使用 file_name 作为集合中的 _id。 但我不想查询 _id 并将其更改为 file_name everytime.Is 有什么方法可以重命名 python 中的名称? 我知道的方法是
data = mongo.collection.find()
for row in data:
row['file_name'] = row.pop('_id')
或
mongo.collection.aggregate({},{'$project':...})
哪个更好。或者其他更好的方法。
来自mongodb4.4+
使用mongo.collection.find({},{'filename':'$_id','_id':0})