PyMongo和集合查询的结果

2024-06-28 20:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在尝试构建一个查询(在我看来,这个查询应该非常简单) 我来自强类型的背景,ide对我的帮助毫无帮助,这一事实令人困惑。 我正在获取一个结果集,并在下一个查询中使用它。没有它抱怨的.toString()选项

#after copy, remove items from source
    def remove_from_source():
        ids = tempCollection.find({}, {"_id": 1})
        q = "{ _id: { $in: [" + ids + "] } }"
        x = sourceCollection.delete_many(q)
        print(x.deleted_count + "records deleted.")

我得到的是

TypeError: can only concatenate str (not "Cursor") to str

但是游标没有字符串选项

编辑 我现在正在这样做,但它仍然不起作用:

# after copy, remove items from source
def remove_from_source():
    ids_cursor = tempCollection.find({}, {"_id": 1})
    ids = ""
    for _id in ids_cursor:
        ids = ids + "ObjectId(" + str(_id['_id']) + "),"
    ids = ids.rstrip(',')
    q = "{ _id: { $in: [" + ids + "] } }"
    print(q)
    # x = sourceCollection.find(q)
    # print(x.count())
    # x = sourceCollection.delete_many(q)
    # print(x.deleted_count + "records deleted.")

如果我把q的输出放在mongo查询中,它的效率是100% 但皮蒙戈对此并不满意。 我可以导入bson ObjectId,但这仍然不允许我以pymongo友好的方式创建q

from bson.objectid import ObjectId

Tags: infromididssource选项countfind