2017-01-24 15 views
0

Ich möchte die folgende SQL-Abfrage konvertieren Mongo:Convert SQL-Abfrage Mongo Abfrage

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; 

I stock_updated_at in Projekt finden möchten, ohne sie auf die Gruppe mit.

Das ist mein mongoquery

db.x.aggregate([  
    { 
     "$group": {  
      "_id": { "Brand": "$Brand"},   
      "TotalStock": { "$sum": 1 },  
      "OutOfStock": { "$sum": .... 
     .... 
    .... 

Antwort

0

Vielleicht können Sie versuchen, https://github.com/alonho/pql

PQL steht für Python-Abfrage-Sprache. PQL übersetzt Python-Ausdrücke in MongoDB-Abfragen.

>>> 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}}]}