从 Mongo 中文档的字段中获取值并删除?

Get values from a field in a document in Mongo and delete?

我想用 CSV 文件中的行更新 Mongo 数据库集合,但我收到与我使用 distinct 有关的错误。如果在 CSV 文件中找到其 ID,我要做的是删除该文档。这是代码:

# Read in CSV file
df = pd.read_csv(csv_path)
# Create list of ID values in CSV
pdToList = list(df['id'])
# Get ids from CSV
for counter, value in enumerate(pdToList):
    # find distinct ids in collection
    result = db[coll_name].distinct("id")
    # Loop through IDs in collection
    for id_val in result:
         # Check if ID in collection equals ID in CSV
         if id_val == value:
             # Delete document if it exists in CSV                         
             db[coll_name].delete_one({'id':id_val})
         else:
             pass

此脚本对大约 100 个表运行良好,但随后我收到一条关于 distinct 太大的错误消息: {'ok': 0.0, 'errmsg': 'distinct too big, 16mb cap', 'code': 17217, 'codeName': 'Location17217'}。你知道我如何从文档中获取字段值而不会出现这个 16mb 上限错误吗?我看过类似的问题,但还没有想出解决办法。

这是我所做的。 find 等于数据框列表中的值的值比尝试执行另一个 for 循环更好,可能对我的计算机也更好。

# Read in CSV file
df = pd.read_csv(csv_path)
# Create list of ID values in CSV
pdToList = list(df['id'])
# Get ids from CSV
for counter, value in enumerate(pdToList):
    # check if value is in document
    if db[coll_name].find({},{"id":{"$eq": value}}):

        # Delete document if it exists in CSV
        db[coll_name].delete_many({'id':value})

    else:
        pass