如何获取数组嵌入数组猫鼬的大小?

Posted

技术标签:

【中文标题】如何获取数组嵌入数组猫鼬的大小?【英文标题】: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
      
    ]
  

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

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

【问题讨论】:

categoryId 的类型必须是 ObjectId 而不是字符串。 ***.com/questions/16310598/… 【参考方案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) 

)

【讨论】:

太棒了,它成功了。太感谢了。我真的很感激!

以上是关于如何获取数组嵌入数组猫鼬的大小?的主要内容,如果未能解决你的问题,请参考以下文章

在猫鼬的子数组中获取具有匹配对象的对象

如何在猫鼬的数组中使用不同类型的模式?

如何从猫鼬的数组中找到对象

从猫鼬的多个集合中获取数据?

如何在猫鼬的嵌入式文档中填充字段?

如何从猫鼬的另一个集合中计算文档?