当对象 id 为 nan 时,如何使用 pymongo 删除 MongoDB 文档?



我有一个mongo数据库。集合中存在一个文档,其中对象 ID 字段为空。我想从数据库中完全删除此集合

records = list(db.MyCollection.find())
for record in records:
if pd.isnull(record['_id']):
print(record)
{'_id': nan, 'other_data': nan}

我试过:

db.Sample.delete_one({'_id': 'nan'})

以及:

db.Sample.delete_one({'_id': np.nan})

尽管这些都没有删除集合。任何建议将不胜感激

它不是null而是nan,实际上表示为float('nan')

db.Sample.delete_one({ '_id': float('nan') })

最新更新