【问题标题】:How to get size of array embedded array mongoose?如何获取数组嵌入数组猫鼬的大小?
【发布时间】:2020-01-02 10:01:10
【问题描述】:

我有一个类别集合,我想通过一些选项通过 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
      }
    ]
  }

但是当我在我的代码上面运行时,结果是 []。

如果你理解我的问题,请帮助我。我搜索了很多,但没有找到解决方案。所以想问问大家。非常感谢。

【问题讨论】:

标签: javascript node.js mongodb mongoose


【解决方案1】:

你的查询没有给你想要的结果,因为 Mongoose does not auto-cast the 24 char hex string to ObjectId in its aggregate pipeline 因为 $project$group 可以以令人惊讶的方式更改架构,因此很难推断应该是 ObjectId

您需要手动转换categoryId 使用mongoose.Types.ObjectId 方法将字符串转换为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) {

})

【讨论】:

  • 太棒了,它成功了。太感谢了。我真的很感激!
猜你喜欢
  • 1970-01-01
  • 2020-10-25
  • 2016-01-21
  • 2018-10-30
  • 2015-10-13
  • 2020-04-15
  • 1970-01-01
  • 2018-06-20
  • 2014-08-13
相关资源
最近更新 更多