简体   繁体   English

猫鼬4.7.5:如何获取数组中的子文档

[英]mongoose 4.7.5: how to fetch subdocuments in array

Am trying to fetch and filter subdocuments in array. 我正在尝试获取和过滤数组中的子文档。

The document has this structure: 该文档具有以下结构:

{
    "_id": {
        "$oid": "58bc4fa0fd85f439ee3ce716"
    },
    "updatedAt": {
        "$date": "2017-03-08T20:39:19.390Z"
    },
    "createdAt": {
        "$date": "2017-03-05T17:49:20.455Z"
    },
    "app": {
        "$oid": "58ae10852035431d5a746cbd"
    },
    "stats": [
        {
            "meta": {
                "key": "value",
                "key": "value"
            },
            "_id": {
                "$oid": "58bc4fc4fd85f439ee3ce718"
            },
            "data": "data",
            "updatedAt": {
                "$date": "2017-03-05T17:49:56.305Z"
            },
            "createdAt": {
                "$date": "2017-03-05T17:49:56.305Z"
            }
        },
        {
            "meta": {
                "key": "value",
                "key": "value"
            },
            "_id": {
                "$oid": "58c06bf79eaf1f15aafe39d0"
            },
            "data": "data",
            "updatedAt": {
                "$date": "2017-03-08T20:39:19.391Z"
            },
            "createdAt": {
                "$date": "2017-03-08T20:39:19.391Z"
            }
        }
    ]
}

What i want to get is the subdocuments in the stats array between two dates I tried mongoose queries chain: 我想要得到的是我尝试过猫鼬查询链的两个日期之间的stats数组中的子文档:

Model.findById(id)
  .select('stats')
  .where('stats.createdAt').gt(data-value).lt(data-value)

But the result always the full document including the all the subdocuments. 但是结果始终是完整的文档,包括所有子文档。

Also I tried aggregation like this: 我也尝试过这样的聚合:

  Model.aggregate({ 
    $match: { 
      'stats.createdAt': '2017-03-05T17:49:56.305Z' 
    } 
  })

The result is always null 结果始终为null

Your result is null because '2017-03-05T17:49:56.305Z' is a String , you are looking for a Date : new Date('2017-03-05T17:49:56.305Z') 您的结果为空,因为'2017-03-05T17:49:56.305Z'是一个String ,您正在寻找一个Datenew Date('2017-03-05T17:49:56.305Z')

You can filter subdocuments with a date range with $unwind and $match : 您可以使用$unwind$match过滤日期范围内的子文档:

var startDate = new Date('2017-03-05T17:49:56.305Z');
var endDate = new Date('2017-03-08T17:49:56.305Z');

Model.aggregate([{
    $match: {
        "_id": new mongoose.mongo.ObjectId("58bc4fa0fd85f439ee3ce716"),
        "stats.createdAt": {
            $gte: startDate,
            $lt: endDate
        }
    }
}, {
    $unwind: "$stats"
}, {
    $match: {
        "stats.createdAt": {
            $gte: startDate,
            $lt: endDate
        }
    }
}], function(err, res) {
    console.log(res);
})

Or more straightforward with $filter : 或更简单的$filter

Model.aggregate([{
    $match: {
        "_id": new mongoose.mongo.ObjectId("58bc4fa0fd85f439ee3ce716"),
        "stats.createdAt": {
            $gte: startDate,
            $lt: endDate
        }
    }
}, {
    $project: {
        "stats": {
            $filter: {
                input: "$stats",
                as: "stat",
                cond: {
                    $and: [
                        { $gte: ["$$stat.createdAt", startDate] },
                        { $lte: ["$$stat.createdAt", endDate)] }
                    ]
                }
            }
        }
    }
}], function(err, res) {
    console.log(res);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM