我想将以下SQL转换为mongo查询:
SELECT brand_name,
count(`inventory`) AS totalstock,
count(if(`inventory`=0, `inventory`, NULL)) as outofstock,
count(if(`inventory`!=0, `inventory`, NULL)) as availablestock,
DATE_ADD(stock_updated_at, INTERVAL 318 minute) as stock_updated_at
FROM x group by Brand order by stock_updated_at desc;
我希望在项目中找到stock_updated_at
而不在群组中使用它。
这是我的mongoquery
db.x.aggregate([
{
"$group": {
"_id": { "Brand": "$Brand"},
"TotalStock": { "$sum": 1 },
"OutOfStock": { "$sum": ....
....
....
答案 0 :(得分:0)
您可以尝试使用现有转换器。 http://www.querymongo.com/
答案 1 :(得分:0)
也许你可以尝试https://github.com/alonho/pql
PQL代表Python-Query-Language。 PQL将python表达式转换为MongoDB查询。
>>> import pql
>>> pql.find("a > 1 and b == 'foo' or not c.d == False")
{'$or': [{'$and': [{'a': {'$gt': 1}}, {'b': 'foo'}]}, {'$not': {'c.d': False}}]}