pymongo 中 $and 运算符的正确用法是什么?
What is the correct use of $and operator in pymongo?
我有这样的结构:
>>>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
您上次查询 returns 文档太多,因为在 python 中您不能真正在字典中包含重复键,例如:
{"$ne": u'-', "$ne":None, "$exists": True}
这会导致第二次出现的 "$ne"
覆盖第一次,以
的字典结尾
{"$ne":None, "$exists": True}
这一切都发生在 python-解释器层,然后传递给 pymongo 驱动程序。
如果您想在单个字段上使用多个 $ne
条件,可以改用 $nin
("not in") 运算符。
来自 MongoDB 关于 $and
用法的文档。
MongoDB provides an implicit AND operation when specifying a comma separated list of expressions. Using an explicit AND with the $and operator is necessary when the same field or operator has to be specified in multiple expressions.
您只需要对重复的字段名称使用 $and
运算符。 humsaver.Disease
、humsaver.dbSNP
和 humsavar
的条件将是隐式的。 (尽管使用 $and
会得到相同的结果)。
test_3.find(
{"humsavar.Disease":
{'$and': [
{"$ne": u'-'},
{ "$ne":None},
{ "$exists": True}
]}
},
{"humsavar.dbSNP":
{'$and':[
{"$ne": u'-'},
{ "$ne": None},
{ "$exists": True}
]},
},
{"humsavar":True, "_id": False}
).count()
我有这样的结构:
>>>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
您上次查询 returns 文档太多,因为在 python 中您不能真正在字典中包含重复键,例如:
{"$ne": u'-', "$ne":None, "$exists": True}
这会导致第二次出现的 "$ne"
覆盖第一次,以
{"$ne":None, "$exists": True}
这一切都发生在 python-解释器层,然后传递给 pymongo 驱动程序。
如果您想在单个字段上使用多个 $ne
条件,可以改用 $nin
("not in") 运算符。
来自 MongoDB 关于 $and
用法的文档。
MongoDB provides an implicit AND operation when specifying a comma separated list of expressions. Using an explicit AND with the $and operator is necessary when the same field or operator has to be specified in multiple expressions.
您只需要对重复的字段名称使用 $and
运算符。 humsaver.Disease
、humsaver.dbSNP
和 humsavar
的条件将是隐式的。 (尽管使用 $and
会得到相同的结果)。
test_3.find(
{"humsavar.Disease":
{'$and': [
{"$ne": u'-'},
{ "$ne":None},
{ "$exists": True}
]}
},
{"humsavar.dbSNP":
{'$and':[
{"$ne": u'-'},
{ "$ne": None},
{ "$exists": True}
]},
},
{"humsavar":True, "_id": False}
).count()