基于内部数组的MongoDb查询

基于内部数组的MongoDb查询,mongodb,Mongodb,我有以下文档结构 Users:[{ "name": "test", "leaveBalance": 20, "leaveHistory": [ { type: "Leave", numberofDays:2, status:"Approved" }, { type: "WFH", numberOfDays:1,

我有以下文档结构

Users:[{
    "name": "test",
    "leaveBalance": 20,
    "leaveHistory": [
       {
          type: "Leave",
          numberofDays:2,
          status:"Approved"
       },
       {
          type: "WFH",
          numberOfDays:1,
          status: "Approved"
       },
       {
           type: "Leave",
           numberOfDays:2,
           status: "Cancelled"
       }
     ]
}, 
{
 ...
}
]
我需要一个查询来查找一个人从此集合中休假的总天数。只有状态为“已批准”且休假类型为“休假”时,才会考虑休假天数。mongodb中是否有查询和查找号码的方法?

请尝试下面的方法

Users.aggregate([
{
    $match:{
        _id:user_id,
        "leaveHistory.type ":"Leave",
        "leaveHistory.status": "Approved"
    }
}, 
{
    unwind:"leaveHistory"
}, 
{
    $project:{
         $count: "numberofDays"
    }   
}
], (err, results)=>{
    console.log(err, results);
})

这是在mongo查询语法中吗?是的,mongo有聚合查询框架,有更多的方法读取更多的数据。查询似乎对我不起作用。它返回的值与leaveHistory中的值相同,而不是每个用户的numberofDays之和。