[英]Using IS NULL in PyMongo
我正在尝试执行'd': None
pymongo查询中'd': None
。 但是当我什么None
做时。 我没有任何结果。
我的SQL:
SELECT date, a, vt, b, c, d
,SUM(theSum) AS theSum
FROM table
where date between '2019-07-08' and '2019-08-08'
AND a in ( 'abc', 'xyz')
AND c IN ('qwe')
AND b IS NOT NULL
AND (d IN ('yo') OR d IS NULL)
group by 1,2,3,4,5,6
order by theSum DESC
LIMIT 25
PyMongo:
{'$match': {
'$and': [{
'date': {
'$gte': sd,
'$lte': ed
},
'a': { '$in': a },
'b': {'$ne': None},
'c': { '$in': c },
}, {
'$or': [{
'd': {'$in': d}
}, {'d': None
}]
}]
}
}
{
'$group': {
'_id': {
'a': '$a',
'vt': '$vt',
'b': '$b',
'c': '$c',
'd': '$d'
},
'theSum':{
'$sum': '$theSum'
}
}
}, {
'$project': {
'_id': 0,
'a':'$_id.a',
'vt': '$_id.vt',
'b': '$_id.b',
'c': '$_id.c',
'd': '$_id.d',
'theSum': '$theSum'
}
}, {
'$sort': { 'theSum': -1 }
}, {
'$limit': 25
}
])
使用SQL查询时,我得到43条记录,但是查询pymongo代码时,我得到0条记录。 我在pymongo中做错了什么。
您应该能够在查询中使用“无”-您可能要看一下方括号-具体来说,在这里:
'$or': [{
'd': {'$in': d}
}, {'d': None
}]
在“ {'d':None”之前有一个括号,而不是在它之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.