我有这样的结构:
>>>test_3.find_one({"humsavar.Disease": {"$exists": True}},
{"humsavar":True, "_id":False})
{u'humsavar': [{u'Association': u'Polymorphism',
u'Disease': u'-',
u'Gene names': u'DTWD1',
u'Mutate aa': u'Pro',
u'Position aa': 9,
u'Reference aa': u'Leu',
u'Substitution': u'Leu9Pro',
u'SwissVarID': u'VAR_036757',
u'Uniprot': u'Q8N5C7',
u'dbSNP': u'rs11539522'},
{u'Association': u'Polymorphism',
u'Disease': u'Pyruvate dehydrogenase lipoic acid synthetase deficiency',
u'Gene names': u'DTWD1',
u'Mutate aa': u'Lys',
u'Position aa': 13,
u'Reference aa': u'Glu',
u'Substitution': u'Glu13Lys',
u'SwissVarID': u'VAR_036758',
u'Uniprot': u'Q8N5C7',
u'dbSNP': u'rs11539519'}]}
我是否应该使用以下查询来搜索 humsavar 中患有疾病和 dbSNP 的所有文档?
test_3.find({"$and": [{"humsavar.Disease": {"$ne": u'-', "$exists": True}},
{"humsavar.dbSNP": {"$ne": u'-', "$ne": None, "$exists": True}}]},
{"humsavar":True, "_id": False}).count()
# output 32
我希望此查询有类似的数字:
test_3.find({"$and": [{"humsavar.Disease": {"$ne": u'-', "$ne":None, "$exists": True}},
{"humsavar.dbSNP": {"$ne": u'-', "$ne": None, "$exists": True}}]},
{"humsavar":True, "_id": False}).count()
但结果是8499
请您参考如下方法:
你的最后一个查询返回了太多文档,因为在 python 中你不能像这样在字典中真正包含重复的键:
{"$ne": u'-', "$ne":None, "$exists": True}
这会导致 "$ne" 的第二次出现覆盖第一个,以字典结束
{"$ne":None, "$exists": True}
这一切都发生在 python 解释器层,然后传递给 pymongo 驱动程序。
如果您想在一个字段上使用多个$ne 条件,您可以使用$nin (“不在”)运算符代替。
