我在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": "..."
}
}]
我想做的是:
maxTime
值的所有元素$max: $time
的最新值($$ROOT
)(现在每id
都是唯一的)source
将所有内容分组为具有该id
(或_id
)我已经实现了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
和一个根元素下的组?
任何想法或见解?我现在卡在这一点上。谢谢!