mongodb聚合在嵌套数组中查找最小值和其他字段

mongodb聚合在嵌套数组中查找最小值和其他字段,mongodb,aggregation-framework,Mongodb,Aggregation Framework,是否可以在嵌套数组中找到最大日期并显示其价格,然后像实际价格一样显示父字段 我希望它的结果如下所示: { "_id" : ObjectId("5547e45c97d8b2c816c994c8"), "actualPrice":19500, "lastModifDate" :ISODate("2015-05-04T22:53:50.583Z"), "price":"16000" } 数据: db.adds.findOne() { "

是否可以在嵌套数组中找到最大日期并显示其价格,然后像实际价格一样显示父字段

我希望它的结果如下所示:

{
    "_id" : ObjectId("5547e45c97d8b2c816c994c8"),
    "actualPrice":19500,
    "lastModifDate" :ISODate("2015-05-04T22:53:50.583Z"),
    "price":"16000"
}
数据:

db.adds.findOne()
    {
            "_id" : ObjectId("5547e45c97d8b2c816c994c8"),
            "addTitle" : "Clio pack luxe",
            "actualPrice" : 19500,
            "fistModificationDate" : ISODate("2015-05-03T22:00:00Z"),
            "addID" : "1746540",
            "history" : [
                    {
                            "price" : 18000,
                            "modifDate" : ISODate("2015-05-04T22:01:47.272Z"),
                            "_id" : ObjectId("5547ec4bfeb20b0414e8e51b")
                    },
                    {
                            "price" : 16000,
                            "modifDate" : ISODate("2015-05-04T22:53:50.583Z"),
                            "_id" : ObjectId("5547f87e83a1dae00bc033fa")
                    },
                    {
                            "price" : 19000,
                            "modifDate" : ISODate("2015-04-04T22:53:50.583Z"),
                            "_id" : ObjectId("5547f87e83a1dae00bc033fe")
                    }
            ],
            "__v" : 1
    }
我的问题

db.adds.aggregate(
    [
    {   $match:{addID:"1746540"}},
    {   $unwind:"$history"},

    {   $group:{
            _id:0,
            lastModifDate:{$max:"$historique.modifDate"}
        }
    }
])
我不知道如何包含我使用$project的其他字段,但我会出错
感谢您的帮助

您可以尝试以下聚合管道,它不需要使用
$group
操作员阶段,因为
$project
操作员负责字段投影:

db.adds.aggregate([
    {   
        "$match": {"addID": "1746540"}
    },
    {
        "$unwind": "$history"
    },        
    {
        "$project": {
            "actualPrice": 1,
            "lastModifDate": "$history.modifDate",
            "price": "$history.price"
        }
    },
    {
        "$sort": { "lastModifDate": -1 }
    },
    {
        "$limit": 1
    }
])
输出

/* 1 */
{
    "result" : [ 
        {
            "_id" : ObjectId("5547e45c97d8b2c816c994c8"),
            "actualPrice" : 19500,
            "lastModifDate" : ISODate("2015-05-04T22:53:50.583Z"),
            "price" : 16000
        }
    ],
    "ok" : 1
}

您可以尝试以下聚合管道,它不需要使用
$group
操作符阶段,因为
$project
操作符负责字段投影:

db.adds.aggregate([
    {   
        "$match": {"addID": "1746540"}
    },
    {
        "$unwind": "$history"
    },        
    {
        "$project": {
            "actualPrice": 1,
            "lastModifDate": "$history.modifDate",
            "price": "$history.price"
        }
    },
    {
        "$sort": { "lastModifDate": -1 }
    },
    {
        "$limit": 1
    }
])
输出

/* 1 */
{
    "result" : [ 
        {
            "_id" : ObjectId("5547e45c97d8b2c816c994c8"),
            "actualPrice" : 19500,
            "lastModifDate" : ISODate("2015-05-04T22:53:50.583Z"),
            "price" : 16000
        }
    ],
    "ok" : 1
}

您可以尝试以下聚合管道,它不需要使用
$group
操作符阶段,因为
$project
操作符负责字段投影:

db.adds.aggregate([
    {   
        "$match": {"addID": "1746540"}
    },
    {
        "$unwind": "$history"
    },        
    {
        "$project": {
            "actualPrice": 1,
            "lastModifDate": "$history.modifDate",
            "price": "$history.price"
        }
    },
    {
        "$sort": { "lastModifDate": -1 }
    },
    {
        "$limit": 1
    }
])
输出

/* 1 */
{
    "result" : [ 
        {
            "_id" : ObjectId("5547e45c97d8b2c816c994c8"),
            "actualPrice" : 19500,
            "lastModifDate" : ISODate("2015-05-04T22:53:50.583Z"),
            "price" : 16000
        }
    ],
    "ok" : 1
}

您可以尝试以下聚合管道,它不需要使用
$group
操作符阶段,因为
$project
操作符负责字段投影:

db.adds.aggregate([
    {   
        "$match": {"addID": "1746540"}
    },
    {
        "$unwind": "$history"
    },        
    {
        "$project": {
            "actualPrice": 1,
            "lastModifDate": "$history.modifDate",
            "price": "$history.price"
        }
    },
    {
        "$sort": { "lastModifDate": -1 }
    },
    {
        "$limit": 1
    }
])
输出

/* 1 */
{
    "result" : [ 
        {
            "_id" : ObjectId("5547e45c97d8b2c816c994c8"),
            "actualPrice" : 19500,
            "lastModifDate" : ISODate("2015-05-04T22:53:50.583Z"),
            "price" : 16000
        }
    ],
    "ok" : 1
}

是,
$group
阶段不必要是,
$group
阶段不必要是,
$group
阶段不必要是,
$group
阶段不必要