0

我正在使用 GridFS 来存储图像。每个图像都有一个group_id元数据,基本上只是定义它们是否是同一原始图像的一部分。因此,每个图像都有多种尺寸,这group_id就是将相同的图像组合在一起的原因。

我想查询这个,但按 group_id 分组。所以它会返回一个 group_id 的数组,每个都有对象的信息(宽度、id 等)。

如果我只是在我想要的记录上进行查找,这就是它的样子:

[ { _id: 4f871d4adaf6fa492f000001,
    width: '500',
    group: '1' },
  { _id: 4f871d4adaf6fa492f000004,
    width: '150',
    group: '1' },
  { _id: 4f871d4bdaf6fa492f000007,
    width: '100',
    group: '1' },
  { _id: 4f871d4bdaf6fa492f00000a,
    width: '50',
    group: '1' },
  { _id: 4f871d51daf6faf42e000001,
    width: '500',
    group: '2' },
  { _id: 4f871d52daf6faf42e000004,
    width: '150',
    group: '2' },
  { _id: 4f871d53daf6faf42e000007,
    width: '100',
    group: '2' },
  { _id: 4f871d53daf6faf42e00000a,
    width: '50',
    group: '2' } ]

我查看了 group 和 mapreduce,并尝试了各种选项,但似乎没有任何效果。任何建议或例子都会很棒!

谢谢!

4

1 回答 1

1

一种可能的方法是使用新聚合框架中的 $group 函数(在 2.1.0 版中可用): http ://www.mongodb.org/display/DOCS/Aggregation+Framework+-+%24group

> db.images.aggregate({$group:{"_id":"$group", "info":{$push:{"width":"$width", "id":"$_id"}}}})
{
    "result" : [
        {
            "_id" : "2",
            "info" : [
                {
                    "width" : "500",
                    "id" : ObjectId("4f871d51daf6faf42e000001")
                },
                {
                    "width" : "150",
                    "id" : ObjectId("4f871d52daf6faf42e000004")
                },
                {
                    "width" : "100",
                    "id" : ObjectId("4f871d53daf6faf42e000007")
                },
                {
                    "width" : "50",
                    "id" : ObjectId("4f871d53daf6faf42e00000a")
                }
            ]
        },
        {
            "_id" : "1",
            "info" : [
                {
                    "width" : "500",
                    "id" : ObjectId("4f871d4adaf6fa492f000001")
                },
                {
                    "width" : "150",
                    "id" : ObjectId("4f871d4adaf6fa492f000004")
                },
                {
                    "width" : "100",
                    "id" : ObjectId("4f871d4bdaf6fa492f000007")
                },
                {
                    "width" : "50",
                    "id" : ObjectId("4f871d4bdaf6fa492f00000a")
                }
            ]
        }
    ],
    "ok" : 1
}
> 

这是您希望达到的结果吗?

使用 Map Reduce 操作也可以实现类似的结果:

var map = function(){
    var info = [{"width":this.width, "id":this._id}]; 
    emit(this.group, {"info":info});
}

var reduce = function(key, values){
    var info = [];
    print(key);
    print(values.length);
    for(var v in values){
        print(values[v]);
        for(var i in values[v].info){
            if(info.indexOf(values[v].info[i]) == -1){
                info.push(values[v].info[i]);
            };
        };
    };
    return {"info":info};
}

> db.images.mapReduce(map, reduce, {out:{inline:1}})
{
    "results" : [
        {
            "_id" : "1",
            "value" : {
                "info" : [
                    {
                        "width" : "500",
                        "id" : ObjectId("4f871d4adaf6fa492f000001")
                    },
                    {
                        "width" : "150",
                        "id" : ObjectId("4f871d4adaf6fa492f000004")
                    },
                    {
                        "width" : "100",
                        "id" : ObjectId("4f871d4bdaf6fa492f000007")
                    },
                    {
                        "width" : "50",
                        "id" : ObjectId("4f871d4bdaf6fa492f00000a")
                    }
                ]
            }
        },
        {
            "_id" : "2",
            "value" : {
                "info" : [
                    {
                        "width" : "500",
                        "id" : ObjectId("4f871d51daf6faf42e000001")
                    },
                    {
                        "width" : "150",
                        "id" : ObjectId("4f871d52daf6faf42e000004")
                    },
                    {
                        "width" : "100",
                        "id" : ObjectId("4f871d53daf6faf42e000007")
                    },
                    {
                        "width" : "50",
                        "id" : ObjectId("4f871d53daf6faf42e00000a")
                    }
                ]
            }
        }
    ],
    "timeMillis" : 1,
    "counts" : {
        "input" : 8,
        "emit" : 8,
        "reduce" : 2,
        "output" : 2
    },
    "ok" : 1,
}
> 

希望这将帮助您实现您想要的结果!

于 2012-04-16T20:27:53.837 回答