我正在尝试在我的 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 中做错了什么。
最佳答案
您应该能够在查询中使用 None - 您可能想看看您的括号 - 具体来说,在这里:
'$or': [{
'd': {'$in': d}
}, {'d': None
}]
“{'d': None”前面有一个括号,而不是后面。
关于python - 在 PyMongo 中使用 IS NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57400442/