簡體   English   中英

將SQL查詢轉換為mongo查詢

[英]Convert SQL query to mongo query

我想將以下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": ....
       ....
   ....

您可以嘗試使用現有的轉換器。 http://www.querymongo.com/

也許您可以嘗試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}}]}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM