mongoDb:聚合文档对子组的引用

时间:2016-02-06 14:53:45

标签: mongodb aggregation-framework

我在mongoDb集合中有一组BSON文档,我想根据表单级联子文档进行处理。有数千个重复的id仅由时间戳和类型为subsystem的文档 - >区分。 component - > subcomponent;它们应该基于source相互依附。这是我目前的样本:

[{
    "id": 1001,
    "type": "subsystem",
    "time": 1454767739,
    "payload": {
        "key": "..."
    }
},
{
    "id": 1001,
    "type": "subsystem",
    "time": 1454767539,
    "payload": {
        "key": "..."
    }
},
{
    "id": 2001,
    "type": "component",
    "time": 1454767778,
    "source": 1001,
    "payload": {
        "key": "..."
    }
},
{
    "id": 5002,
    "type": "subcomponent",
    "time": 1454767779,
    "source": 2001,
    "payload": {
        "key": "..."
    }
},
{
    "id": 5003,
    "type": "subcomponent",
    "time": 1454767798,
    "source": 2001,
    "payload": {
        "key": "..."
    }
}]

我想做的是:

  1. 匹配时间戳低于maxTime值的所有元素
  2. 按(唯一)ID对所有元素进行分组,但仅使用$max: $time的最新值($$ROOT)(现在每id都是唯一的)
  3. 按属性source将所有内容分组为具有该id(或_id
  4. 的元素的数组

    我已经实现了1.)和2.)已经有了这个小组:

    [{
        "_id": 1001,
        "reftime": 1454767739,
        "element": [
            {
                "id": 1001,
                "type": "subsystem",
                "time": 1454767739,
                "payload": {
                    "key": "..."
                }
            }
        ]
    
    },
    {
        "_id": 2001,
        "reftime": 1454767778,
        "element": [
            {
                "id": 2001,
                "type": "component",
                "time": 1454767778,
                "source": 1001,
                "payload": {
                    "key": "..."
                }
            }
    
        ]
    },
    {
        "_id": 5002,
        "reftime": 1454767779,
        "element": [
            {
                "id": 5002,
                "type": "subcomponent",
                "time": 1454767779,
                "source": 2001,
                "payload": {
                    "key": "..."
                }
            }
        ]
    },
    {
        "_id": 5003,
        "reftime": 1454767798,
        "element": [
            {
                "id": 5003,
                "type": "subcomponent",
                "time": 1454767798,
                "source": 2001,
                "payload": {
                    "key": "..."
                }
            }
        ]
    }]
    

    但我想要的是根据source值中的链接构建的元素。所以它看起来像这样:

    {
        "_id": 1001,
        "reftime": 1454767739,
        "element": [
            {
                "id": 1001,
                "type": "subsystem",
                "time": 1454767739,
                "payload": {
                    "key": "..."
                }
            }
        ],
        "attached": [
            {
                "_id": 2001,
                "reftime": 1454767778,
                "element": [
                    {
                        "id": 2001,
                        "type": "component",
                        "time": 1454767778,
                        "source": 1001,
                        "payload": {
                            "key": "..."
                        }
                    }
                ],
                "attached": [
                    {
                        "_id": 5002,
                        "reftime": 1454767779,
                        "element": [
                            {
                                "id": 5002,
                                "type": "subcomponent",
                                "time": 1454767779,
                                "source": 2001,
                                "payload": {
                                    "key": "..."
                                }
                            }
                        ]
                    },
                    {
                        "_id": 5003,
                        "reftime": 1454767798,
                        "element": [
                            {
                                "id": 5003,
                                "type": "subcomponent",
                                "time": 1454767798,
                                "source": 2001,
                                "payload": {
                                    "key": "..."
                                }
                            }
                        ]
                    }                
    
                ]
            }        
        ]
    
    }
    

    这可能吗?我正在努力组建一个包含source的所有内容的组,并将其与样本2中的组合在一起。)。或者,如果这会有所帮助,我可以介绍source: 0?对于每个subsystem和一个根元素下的组?

    任何想法或见解?我现在卡在这一点上。谢谢!

0 个答案:

没有答案