我正在使用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
提交
取消