1

我有一些数据存储在 mongodb 集合中,类似于:

{"_id": 1, "category": "food", "name": "chips", "price": 1.50, "effectiveDate": ISODate("2013-03-01T07:00:00Z")} 
{"_id": 2, "category": "food", "name": "chips", "price": 1.75, "effectiveDate": ISODate("2013-03-05T07:00:00Z")}
{"_id": 3, "category": "food", "name": "chips", "price": 1.90, "effectiveDate": ISODate("2013-03-10T07:00:00Z")}
{"_id": 4, "category": "beverage", "name": "pop", "price": 2.00, "effectiveDate": ISODate("2013-03-01T07:00:00Z")}
{"_id": 5, "category": "beverage", "name": "pop", "price": 2.25, "effectiveDate": ISODate("2013-03-05T07:00:00Z")}
{"_id": 6, "category": "beverage", "name": "pop", "price": 1.80, "effectiveDate": ISODate("2013-03-10T07:00:00Z")}

在 mongodb 中,我将如何编写一个查询来返回在特定日期处于活动状态的文档,按类别分组?

如果我指定 2013 年 3 月 6 日,我希望看到结果:

{"_id": 2, "category": "food", "name": "chips", "price": 1.75, "effectiveDate": ISODate("2013-03-05T07:00:00Z")}
{"_id": 5, "category": "beverage", "name": "pop", "price": 2.25, "effectiveDate": ISODate("2013-03-05T07:00:00Z")}

我是 mongo 的新手,一直在尝试使用组、聚合和 mapreduce 来做到这一点,但一直在绕圈子。

4

1 回答 1

1

为了给你一个真正好的答案,我需要更多关于你的代码和你想要做什么的细节。但如果我理解正确,我认为你可以只使用聚合框架来解决这个问题。你应该知道聚合框架使用了管道的概念,也就是说,每一步的结果都作为下面的入口。

我的查询:

db.yourcollection.aggregate([

    /* First exclude everything that is superior to a given date */
    {$match:{effectiveDate:{$lte:new Date(2013,2,6)}}},

    /* Sort the rest by date, descending */
    {$sort:{effectiveDate:-1}},

    /* Group by name+category and grab only the first result
       (the newest below that date) */
    {$group:{_id:{name:'$name',category:'$category'}, effectiveDate:{$first:"$effectiveDate"},price:{$first:"$price"}}},

    /* You said you want the results grouped by category.
       This last $group does that and returns all matching products inside an array
       It also removes the duplicates */
    {$group:{_id:'$_id.category',products:{$addToSet:{name:"$_id.name",price:"$price",effectiveDate:"$effectiveDate"}}}}

])

输出是这样的:

{
    "result": [
        {
            "_id": "food",
            "products": [
                {
                    "name" : "chips",
                    "price" : 1.75,
                    "effectiveDate" : ISODate("2013-03-05T07:00:00Z")
                }
            ]
        },
        {
            "_id" : "beverage",
            "products": [
                {
                    "name" : "pop",
                    "price" : 2.25,
                    "effectiveDate" : ISODate("2013-03-05T07:00:00Z")
                }
            ]
        }
    ],
    "ok":1
}

您可以更改最终输出修改最后一个$group或使用$project

于 2013-03-15T19:53:58.893 回答