mongodb中带重复的数组反聚集
我想对一个有一条记录的mongo集合进行反聚合,其中包含一个大的列表,取而代之的是将其表示为另一个集合中的多条记录。对于长数组中不包含的记录中的变量,这将意味着在将每个新记录复制到新集合时,在arary上方将顶层重复到每个新记录中 我所拥有的是:mongodb中带重复的数组反聚集,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我想对一个有一条记录的mongo集合进行反聚合,其中包含一个大的列表,取而代之的是将其表示为另一个集合中的多条记录。对于长数组中不包含的记录中的变量,这将意味着在将每个新记录复制到新集合时,在arary上方将顶层重复到每个新记录中 我所拥有的是: > db.current.showOne() {"name" : "thing", "othervar" : 1, "longcollection" : [ {"first": 1,
> db.current.showOne()
{"name" : "thing",
"othervar" : 1,
"longcollection" : [
{"first": 1,
"second":2},
{"first": 3,
"second":4},
{"first": 5,
"second":6},
... etc...
{"first": 10000,
"second":10001}
]
}
> db.new.find().limit(5000).pretty()
{"name" : "thing",
"othervar" : 1,
"longcollection" :
{"first": 1,
"second":2}
},
{"name" : "thing",
"othervar" : 1,
"longcollection" :
{"first": 3,
"second":4}
},
{"name" : "thing",
"othervar" : 1,
"longcollection" :
{"first": 5,
"second":6}
},
{"name" : "thing",
"othervar" : 1,
"longcollection" :
{"first": 7,
"second":8}
}
我想要的是:
> db.current.showOne()
{"name" : "thing",
"othervar" : 1,
"longcollection" : [
{"first": 1,
"second":2},
{"first": 3,
"second":4},
{"first": 5,
"second":6},
... etc...
{"first": 10000,
"second":10001}
]
}
> db.new.find().limit(5000).pretty()
{"name" : "thing",
"othervar" : 1,
"longcollection" :
{"first": 1,
"second":2}
},
{"name" : "thing",
"othervar" : 1,
"longcollection" :
{"first": 3,
"second":4}
},
{"name" : "thing",
"othervar" : 1,
"longcollection" :
{"first": 5,
"second":6}
},
{"name" : "thing",
"othervar" : 1,
"longcollection" :
{"first": 7,
"second":8}
}
……等等
每个记录的唯一信息位于“longcollection”变量中,该变量现在是一个字典,而不是一个数组。其他信息与“longcollection”处于同一级别,而不是在其内部,对所有新记录重复
我认为这是一种展开,或者说是放松。copyTo()和unwrap/aggregation的语法组合是什么?我对mongodb的javascript和聚合方面还是有点陌生。您应该可以通过一个简单的 对于上述示例,您可以使用:
db.current.aggregate({$unwind: "$longcollection"})
这将为您提供如下结果:
{
result: : [
{
"_id" : ObjectId(...),
"name": xxxx,
"othervar": yyyyy,
"longcollection" : {
"first": 1,
"second":2
}
},
{
"_id" : ObjectId(...),
"name": yyyy,
"othervar": zzzz,
"longcollection" : {
"first": 3,
"second":4
}
}],
"ok" : 1
}
要停止评论中出现的重复_id消息,您应该能够使用:
db.current.aggregate({$project : {_id: 0, name: 1, othervar: 1, longcollection: 1}}, {$unwind: "$longcollection"})
您应该能够通过一个简单的 对于上述示例,您可以使用:
db.current.aggregate({$unwind: "$longcollection"})
这将为您提供如下结果:
{
result: : [
{
"_id" : ObjectId(...),
"name": xxxx,
"othervar": yyyyy,
"longcollection" : {
"first": 1,
"second":2
}
},
{
"_id" : ObjectId(...),
"name": yyyy,
"othervar": zzzz,
"longcollection" : {
"first": 3,
"second":4
}
}],
"ok" : 1
}
要停止评论中出现的重复_id消息,您应该能够使用:
db.current.aggregate({$project : {_id: 0, name: 1, othervar: 1, longcollection: 1}}, {$unwind: "$longcollection"})
因此,在完成我的示例后,我很难将其放入一个新集合:
db.current.aggregate({$unwind:$longcollection}).result.forEach(函数(x){db.new.insert(x)}
给出E11000重复键错误索引:db.new.$\u id\udup键:{:ObjectId('51f09dd26f73203b33ca83'))
在insert语句开始工作之前添加删除x.\u id
。是的,带上\u id字段意味着您在尝试插入时将获得重复的\u id。您可以使用将_id字段从未创建的数据集合中排除,从而使它不会进入$unwind。因此,完成我的示例后,我很难将其放入新集合:db.current.aggregate({$unwind:$longcollection”})。result.forEach(函数(x){db.new.insert(x)})
给出E11000重复键错误索引:db.new.$\u id\u dup key:{:ObjectId('51f09dd26f7320383b33ca83')}
在insert语句开始工作之前添加delete x.\u id
是的,带上_id字段意味着在尝试插入时将获得重复的_id。通过使用将_id字段从正在展开的数据集合中排除,可以使它不进入$unwind