2015-04-28 36 views
1

一个MongoDB的查询下面的查询MongoDB中壳工作:如何编写pymongo

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。

+0

你能更具体地说明你如何得到不同的答案吗? –

+0

对于一个mongo shell查询约为11,000,对于python脚本查询约为17,000。 – dwstein

回答

0

你应该使用:

"$exists": True 

,而不是

"$exists": "true" 

同为$ne