【问题标题】:Aggregate Query in MongoDBMongoDB 中的聚合查询
【发布时间】:2017-08-06 06:43:39
【问题描述】:

我是 MongoDB 的新手。我需要从一系列产品中获取特定日期的最新更新产品。 下面是我要求的 JSON 对象。

{
    "_id": ObjectId("10001"),
    "product": [{
        "image": "./../../../prod1.html",
        "name": "product",
        "version": 1,
        "updatedDate": "14-03-2017"
    }, {
        "image": "./../../../prod1.html",
        "name": "product",
        "version": 2,
        "updatedDate": "14-03-2017"
    }, {
        "image": "./../../../prod1.html",
        "name": "product",
        "version": 1,
        "updatedDate": "15-03-2017"
    }, {
        "image": "./../../../prod1.html",
        "name": "product",
        "version": 2,
        "updatedDate": "15-03-2017"
    }, {
        "image": "./../../../prod1.html",
        "name": "product",
        "version": 3,
        "updatedDate": "15-03-2017"
    }, {
        "image": "./../../../prod1.html",
        "name": "product",
        "version": 4,
        "updatedDate": "15-03-2017"
    }]
}, {
    "_id": ObjectId("10002"),
    "product": [{
        "image": "./../../../prod1.html",
        "name": "product",
        "version": 1,
        "updatedDate": "14-03-2017"
    }, {
        "image": "./../../../prod1.html",
        "name": "product",
        "version": 2,
        "updatedDate": "14-03-2017"
    }, {
        "image": "./../../../prod1.html",
        "name": "product",
        "version": 1,
        "updatedDate": "15-03-2017"
    }, {
        "image": "./../../../prod1.html",
        "name": "product",
        "version": 2,
        "updatedDate": "15-03-2017"
    }, {
        "image": "./../../../prod1.html",
        "name": "product",
        "version": 3,
        "updatedDate": "15-03-2017"
    }]
},
}

我需要一个查询 - 它将检索 14-03-2017 的最新版本的产品。

预期结果是:

{
"_id" : ObjectId("10001"),
"product" : [
    {"image" : "./../../../prod1.html","name" : "product","version" : 4,"updatedDate":"15-03-2017"} 
]},
 {
"_id" : ObjectId("10002"),
"product" : [
    {"image" : "./../../../prod1.html","name" : "product","version" : 3,"updatedDate":"15-03-2017"} 
]}

非常感谢有人可以帮助我使用聚合函数来获得预期的结果。

【问题讨论】:

  • 需要获取最新日期和该日期最新版本的产品(日期将包含多个版本)
  • 您的 MongoDB 服务器版本是多少?

标签: mongodb mongodb-query aggregation-framework


【解决方案1】:

使用聚合命令,

db.collection.aggregate([{
    $unwind: "$product"
}, {
    $sort: {
        "product.updatedDate": 1
    }
}, {
    $group: {
        _id: "$_id",
        product: {
            $last: "$product"
        }
    }
}])

输出:

{
    "_id" : ObjectId("10002"),
    "product" : {
        "image" : "./../../../prod1.html",
        "name" : "product",
        "version" : 3,
        "updatedDate" : "15-03-2017"
    }
}
{
    "_id" : ObjectId("10001"),
    "product" : {
        "image" : "./../../../prod1.html",
        "name" : "product",
        "version" : 4,
        "updatedDate" : "15-03-2017"
    }
}

【讨论】:

  • 朋友您好,非常感谢。它工作得很好。感谢您的即时回复。
猜你喜欢
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多