我正在使用PyMongo从收藏夹中获取文档。一切工作正常,直到我尝试将结果限制为仅在选择了某个集合的情况下才获取存在特定字段的文档。这是我的代码query["is_approved"] = Noneif source_collection == "collection-name": query["field_to_check_for"]['exists'] = Truesort = [("created_date", -1)]cursor = c.find(query, sort=sort).limit(20)上面的代码在与query [“ field_to_check_for”] ['exists'] =真我也尝试使用query [“ field_to_check_for”] =“存在”但这会返回一个空结果
1 回答

浮云间
TA贡献1829条经验 获得超4个赞
您的查询字典格式错误,请尝试以下一种:
query = {"field_to_check_for": {"$exists": True}}
cursor = db.collection-name.find(query)
添加回答
举报
0/150
提交
取消