Агрегировать неожиданный токен: - PullRequest
0 голосов
/ 20 мая 2018

Ниже агрегированный запрос mongodb дает

Неожиданный токен: ошибка

db.getCollection("products_data").aggregate(
    {
        "$unwind": {
            "path": "$color",
            "preserveNullAndEmptyArrays": true
        }
    },
    {
       "$match":{
        "country":"UK",
        "$or":[{
            "$and":[
                "$or":
                    [{
                        "$and":[
                            {"status":"drafted"},
                            {"color":{$in:["blue"]}}
                        ]},
                        {"$and":[
                            {"status1":"complete"},
                            {"status2":{$nin:["n/a","drafted","complete"]}},
                            {"color":{$in:["green"]}}

                    ]}
                ]
            ]
            },{
            "$and":[
                "$or":
                    [
                       { "$and":[
                            {"status":"drafted"},
                            {"color":{$in:["blue"]}}
                        ]},
                        {"$and":[
                            {"status1":"complete"},
                            {"status2":{$nin:["n/a","drafted","complete"]}},
                            {"color":{$in:["green"]}}

                    ]}
                ]
            ]
            }
        ]
       }

    },


    {
    "$group":{
        "_id":"$field",
        "products":{$sum: 1},
        "bid":{"$push":"$product_id"}
    }
},
{
    "$project":{
        "field":"$_id",
        "products":"$products",
        "bid":1,
        "_id":0
    }
}
);

Извлечь статистическое число для заданного указанного условия.

1 Ответ

0 голосов
/ 20 мая 2018

Правильный синтаксис для использования aggregate и его этапы в pipeline

db.getCollection("products_data").aggregate([
  { "$unwind": { "path": "$color", "preserveNullAndEmptyArrays": true }},
  { "$match": {
    "country": "UK",
    "$or": [
      {
        "$and": [
          {
            "$or": [
              { "$and": [{ "status": "drafted" }, { "color": { "$in": ["blue"] }}] },
              { "$and": [{ "status1": "complete" }, { "status2": { "$nin": ["n/a", "drafted", "complete"] }}, { "color": { "$in": ["green"] }}]}
            ]
          }
        ]
      },
      {
        "$and": [
          {
            "$or": [
              { "$and": [{ "status": "drafted" }, { "color": { "$in": ["blue"] }}] },
              { "$and": [{ "status1": "complete" }, { "status2": { "$nin": ["n/a", "drafted", "complete"] }}, { "color": { "$in": ["green"] }}] }
            ]
          }
        ]
      }
    ]
  }},
  { "$group": {
    "_id": "$field",
    "products": { "$sum": 1 },
    "bid": { "$push": "$product_id" }
  }},
  { "$project": { "field": "$_id", "products": "$products", "bid": 1, "_id": 0 }}
])
...