簡體   English   中英

如何獲取數組嵌入數組貓鼬的大小?

[英]How to get size of array embedded array mongoose?

我有一個類別集合,我想通過一些選項按 id 獲取類別。 這是數據庫中集合的結構。

[
  {
    "_id": "5d67296bf35b984e74486924",
    "name": "Dinrk",
    "images": [],
    "recipes": [
      {
        "name": "Coffee",
        "time": 20,
        "img": "https://google.com/image-example.jpg",
        "des": "This is description",
        "serving": 2,
        "components": [
          {
            "name": "Dink 1",
            "quantity": "1"
          },
          {
            "name": "Dink 2",
            "quantity": "1"
          },
          {
            "name": "Dink 2",
            "quantity": "1"
          }
        ],
        "cook_steps": [
          {
            "des": "This is description",
            "pictures": []
          },
          {
            "des": "This is description",
            "pictures": []
          }
        ]
      },
      {
        "name": "Coffee",
        "time": 20,
        "img": "https://google.com/image-example.jpg",
        "des": "This is description",
        "serving": 2,
        "components": [
          {
            "name": "Dink 1",
            "quantity": "1"
          },
          {
            "name": "Dink 2",
            "quantity": "1"
          }
        ],
        "cook_steps": [
          {
            "des": "This is description",
            "pictures": []
          },
          {
            "des": "This is description",
            "pictures": []
          }
        ]
      }
    ]
  },
  {
    "_id": "5d67296bf35b984e74486435555",
    "name": "Cake",
    "images": [],
    "recipes": [
      {
        "name": "Cake",
        "time": 20,
        "img": "https://google.com/image-example.jpg",
        "des": "This is description",
        "serving": 2,
        "components": [
          {
            "name": "Cake 1",
            "quantity": "1"
          },
          {
            "name": "Cake 2",
            "quantity": "1"
          },
          {
            "name": "Cake 2",
            "quantity": "1"
          }
        ],
        "cook_steps": [
          {
            "des": "This is description",
            "pictures": []
          },
          {
            "des": "This is description",
            "pictures": []
          }
        ]
      },
      {
        "name": "Coffee",
        "time": 20,
        "img": "https://google.com/image-example.jpg",
        "des": "This is description",
        "serving": 2,
        "components": [
          {
            "name": "Cake 1",
            "quantity": "1"
          }
        ],
        "cook_steps": [
          {
            "des": "This is description",
            "pictures": []
          },
          {
            "des": "This is description",
            "pictures": []
          }
        ]
      }
    ]
  }
]

這是我嘗試 categoryId = "5d67296bf35b984e74486924" 的代碼

Category.aggregate([
            {
                $match: {'_id': categoryId}
            },
            {
                $unwind: '$recipes'
            },
            {
                $project: {
                    'total_components': {'$size': '$recipes.components'},
                    'total_cook_steps': {'$size': '$recipes.cook_steps'}
                }
            }
        ]).then(function(data) {

}, function(err) {

})

預期的結果是

{
    "_id": "5d67296bf35b984e74486924",
    "name": "Dinrk",
    "images": [],
    "recipes": [
      {
        "name": "Coffee",
        "time": 20,
        "img": "https://google.com/image-example.jpg",
        "des": "This is description",
        "serving": 2,
        "total_components": 3,
        "total_cook_steps": 2
      },
      {
        "name": "Coffee",
        "time": 20,
        "img": "https://google.com/image-example.jpg",
        "des": "This is description",
        "serving": 2,
        "total_components": 2,
        "total_cook_steps": 2
      }
    ]
  }

但是當我在我的代碼上方運行時,結果是 []。

如果您了解我的問題,請幫助我。 我搜索了很多,但沒有找到解決方案。 所以想問問大家。 非常感謝。

您的查詢沒有給您想要的結果,因為 Mongoose不會在其聚合管道中將 24 個字符的十六進制字符串自動轉換為 ObjectId,因為$project$group可以以令人驚訝的方式更改架構,以至於很難推斷出應該是什么ObjectId

您需要使用mongoose.Types.ObjectId方法手動將categoryId字符串轉換為ObjectId

$map運算符而不是$unwind 中計算新字段,因為這允許您使用更少的管道步驟進行聚合操作

Category.aggregate([
    { '$match': { '_id': mongoose.Types.ObjectId(categoryId) } },
    { '$addFields': {
        'recipes': {
            '$map': {
                'input': '$recipes',
                'in': {
                    'name': '$$this.name',
                    'time': '$$this.time',
                    'img': '$$this.img',
                    'des': '$$this.des',
                    'serving': '$$this.serving',
                    'total_components': { '$size': '$$this.components' },
                    'total_cook_steps': { '$size': '$$this.cook_steps' }
                }
            }
        }
    } }
]).then(function(data) {

}, function(err) {

})

暫無
暫無

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

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