MongoDB:返回具有最新日期的嵌套数组中的对象

时间:2016-03-21 18:47:11

标签: mongodb

我有以下系列:

{ 
    "_id" : ObjectId("56f036e032ea1f27634e2f1f"), 
    "mockups" : [
        {
            "versions" : [
                {
                    "title" : "About us 1", 
                    "timestamp" : "2016-01-10T12:31:23.104Z", 
                    "_id" : ObjectId("56ec65a9041c87dd6bd17922")
                }, 
                {
                    "title" : "About us 3", 
                    "timestamp" : "2016-03-11T15:34:11.108Z", 
                    "_id" : ObjectId("56ec65a9041c87dd6bd17923")
                }, 
                {
                    "title" : "About us 2", 
                    "timestamp" : "2016-02-21T16:15:23.101Z", 
                    "_id" : ObjectId("56ec65a9041c87dd6bd17924")
                }
            ], 
            "_id" : ObjectId("56ec65a9041c87dd6bd17921")
        }, 
        {
            "versions" : [
                {
                    "title" : "Contact us 1", 
                    "timestamp" : "2016-04-10T11:34:33.103Z", 
                    "_id" : ObjectId("56ec65a9041c87dd6bd17924")
                }, 
                {
                    "title" : "Contact us 3", 
                    "timestamp" : "2016-06-21T16:13:26.101Z", 
                    "_id" : ObjectId("56ec65a9041c87dd6bd17926")
                }, 
                {
                    "title" : "Contact us 2", 
                    "timestamp" : "2016-05-11T13:34:13.106Z", 
                    "_id" : ObjectId("56ec65a9041c87dd6bd17925")
                }
            ], 
            "_id" : ObjectId("56ec65a9041c87dd6bd17929")
        }
    ]
}

我想将所有mockups与最新的version一起返回,这会产生类似于以下内容的内容:

{ 
    "_id" : ObjectId("56f036e032ea1f27634e2f1f"), 
    "mockups" : [
        {
            "versions" : [
                {
                    "title" : "About us 3", 
                    "timestamp" : "2016-03-11T15:34:11.108Z", 
                    "_id" : ObjectId("56ec65a9041c87dd6bd17923")
                }
            ], 
            "_id" : ObjectId("56ec65a9041c87dd6bd17921")
        }, 
        {
            "versions" : [
                {
                    "title" : "Contact us 3", 
                    "timestamp" : "2016-06-21T16:13:26.101Z", 
                    "_id" : ObjectId("56ec65a9041c87dd6bd17926")
                }
            ], 
            "_id" : ObjectId("56ec65a9041c87dd6bd17929")
        }
    ]
}

我一直在玩aggregatesortlimit,但我对mongodb非常陌生。我目前只需返回所有内容并使用lodash之类的内容来获取我需要的版本。

有没有办法正确查询,以便我从mongo获得所需的结果,而不是在我得到结果后使用lodash之类的内容?

2 个答案:

答案 0 :(得分:0)

我实际上并没有理解"最新"的范围是什么?版本,但我认为你可以使用这样的东西: 例如:

db.collection.find({
    versions.timestamp: {
        $gte: ISODate("2010-04-29T00:00:00.000Z"),
        $lt: ISODate("2010-05-01T00:00:00.000Z")
    }
}) 

如果您想使用aggregate,可以将此条件添加到$match

答案 1 :(得分:0)

对于使用聚合的解决方案,建议运行以下管道以获得所需的输出:

db.collection.aggregate([
    { "$unwind": "$mockups" },
    { "$unwind": "$mockups.versions" },
    { "$sort": { "mockups.versions.timestamp": -1 } },
    {
        "$group": {
            "_id": "$mockups._id",                          
            "title": { "$first": "$mockups.versions.title" },
            "timestamp": { "$first": "$mockups.versions.timestamp" },
            "id" : { "$first": "$mockups.versions._id" },
            "main_id": { "$first": "$_id" }
        }
    },
    {
        "$group": {
            "_id": "$_id",
            "versions": {
                "$push": {
                    "title": "$title",
                    "timestamp": "$timestamp",
                    "_id": "$id" 
                }        
            },
            "main_id": { "$first": "$main_id" }
        }
    },
    {
        "$group": {
            "_id": "$main_id",
            "mockups": {
                "$push": {
                    "versions": "$versions",                    
                    "_id": "$_id" 
                }        
            }        
        }
    }    
])