Mongodb按dbref字段分组 [英] Mongodb group by dbref field

查看:150
本文介绍了Mongodb按dbref字段分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按型号分组的产品.每个产品都有模型字段-DBRef to Models集合.我尝试使用此聚合查询,但出现错误FieldPath field names may not start with '$'.

I need group products by model. Each product has model field - DBRef to Models collection. I tried use this aggregate query, but have error FieldPath field names may not start with '$'.

汇总查询:

db.Products.aggregate([
    { $project: { _id: 0, model: 1, isActive: 1 } },
    { $group: { _id: "$model.$id", actives: { $push: "$isActive" } }}
]);

产品文档示例:

{
    _id: ObjectId("54f48610e31701d2184dede5"),
    isActive: true,
    model: {
        $db: "database",
        $ref: "Models",
        $id: ObjectId("....")
    }
}

推荐答案

手册中曾经有一节明确指出,聚合框架不支持DBRef以及其他各种BSON类型.

There used to be a section in the manual that explicity stated that DBRef is not supported under the aggregation framework, along with various other BSON types.

此段落 Google网上论坛归档文件 Google网上论坛归档消息:

The old passage read as shown in this google groups archive message:

警告::管道无法对以下类型的值进行操作:BinarySymbolMinKeyMaxKeyDBRefCode.

Warning: The pipeline cannot operate on values of the following types: Binary, Symbol, MinKey, MaxKey, DBRef, Code, and CodeWScope.

它可能仍在某处,但我似乎现在找不到它了:)

It might still be there somewhere but I just cannot seem to find it right now :)

该消息线程中的另一个优点是,除了聚合框架不支持此功能外,另一个选择(也是聚合的唯一实选项)是使用

Also as aluded to in that message thread is that aside from this not being supported in the aggregation framework, then your other option ( and only real option for aggregation ) is to use the mapReduce method instead. As a shell example:

db.Products.mapReduce(
    function() {
        emit( this.model.$id, { "actives": [this.isActive] } );
    },
    function(key,values) {
        var result = { "actives": [] };
        values.forEach(function(value) {
            value.actives.forEach(function(active) {
                result.actives.push( active );
            });
        });
    },
    { "out": { "inline": 1 } }
)

由于mapReduce结果的任意{ "_id": "", "value": { } }结构,它看起来不太好,但是它确实允许您要查找的聚合类型.

It doesn't look as nice because of the arbitrary { "_id": "", "value": { } } structure of mapReduce results, but it does allow the sort of aggregation you are looking for.

也有对此JIRA问题的引用: SERVER-14466 ,但是我会在这方面的动作不多.

There is also reference to this JIRA Issue: SERVER-14466, but I would not hold out for much movement on that front.

因此,您可以使用mapReduce,但建议您不要使用DBRef,而是定义另一种形式的手动引用",既可以将集合"和数据库"信息嵌入到应用程序中,也可以依赖于此类内容的外部定义模式,取决于您的需求.只要在那里遵循相同的规则,就可以将聚合框架用于具有有效属性名称的任何内容.

So you can use mapReduce but it would be advised to move away from using DBRef and define an alternate form of "manual references" either embedding "collection" and "database" information or relying on external definition of such things in your application schema, depending on your needs. As long as you follow the same rules there then you can use the aggregation framework for anything with valid property names.

这篇关于Mongodb按dbref字段分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆