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

时间:2021-02-19 21:19:35

Is it possible to find in a nested array the max date and show its price then show the parent field like the actual price.

是否有可能在嵌套数组中找到最大日期并显示其价格,然后显示父字段,如实际价格。

The result I want it to show like this :

结果我希望它显示如下:

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

The data :

数据 :

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
    }

my query

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

    {   $group:{
            _id:0,
            lastModifDate:{$max:"$historique.modifDate"}
        }
    }
])

I dont know how to include other fields I used $project but I get errors thanks for helping

我不知道如何包含我使用$ project的其他字段但是我得到错误感谢帮助

1 个解决方案

#1


You could try the following aggregation pipeline which does not need to make use of the $group operator stage as the $project operator takes care of the fields projection:

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

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

Output

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

#1


You could try the following aggregation pipeline which does not need to make use of the $group operator stage as the $project operator takes care of the fields projection:

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

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

Output

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