簡體   English   中英

Mongodb 匯總 $facet

[英]Mongodb aggregate $facet

我正在嘗試使用聚合管道的 $facet 階段來匹配和分組文檔,但我懷疑我的語法不正確。

誰能告訴我這有什么問題嗎? 不斷收到消息“流水線階段規范 object 必須包含確切的一個字段”。

[
  {
   '$match': {
   'Id': 59
  }
  }, {
   '$unwind': {
   'path': '$Vehicles'
 }
 }, {

    // It's this stage that's playing up.
    '$facet': {
       'Offers': [
         {
            '$match': { 'Vehicles.VehicleGrouping': 'OOTW' }, 
            '$group': {
                '_id': '$Vehicles.Manufacturer', 
                'count': { '$sum': 1 }
            }
         }
       ]
     }
     // End of problematic stage.

   }
 ]

謝謝。

您已將$match$group放在一個 object 中,試試這個:

db.collection.aggregate([
  {
    "$match": {
      "Id": 59
    }
  },
  {
    "$unwind": {
      "path": "$Vehicles"
    }
  },
  {
    // It's this stage that's playing up.
    "$facet": {
      "Offers": [
        {
          "$match": {
            "Vehicles.VehicleGrouping": "OOTW"
          }
        },
        {
          "$group": {
            "_id": "$Vehicles.Manufacturer",
            "count": {
              "$sum": 1
            }
          }
        }
      ]
    }  
  }
])

暫無
暫無

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

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