簡體   English   中英

如何在pymongo中編寫mongodb查詢

[英]how to write a mongodb query in pymongo

以下查詢在mongodb shell中起作用:

db.user.count( {$and: [ {"agent_id":{$exists:true}}, {"is_agent":{$ne:true}} ] } )

當我在python中嘗試時,我得到了不同的答案。 這是python代碼:

import pymongo
from pymongo import MongoClient

def getCollection(cient,dbname,collection):
    """Return a colleciton based on client, db and collection"""
    data_base = getattr(client, dbname)
    collObject = getattr(data_base, collection)
    return collObject   

userColl = getCollection(client, "hkpr_restore","user")

usersWithAgents = userColl.count( {"$and": [ {"agent_id":{"$exists":"true"}}, {"is_agent":{"$ne":"true"}} ] } )

print usersWithAgents

mongo shell查詢的結果約為11,000,而python腳本查詢的結果約為17,000。

您應該使用:

"$exists": True

並不是

"$exists": "true"

$ne

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM