检索 mongodb 中集合的所有值
Retrieve all values of a collection in mongodb
我希望在一个集合中检索我的集合的所有值:
示例:
` "_id" : {
"origin" : "xx",
"destination" : "yy"
},
"paths" : [
[
"tt"
]
]
}
/* 2 */
{
"_id" : {
"origin" : "aa",
"destination" : "bb"
},
"paths" : [
[
"cc"
]
]
}
/* 3 */
{
"_id" : {
"origin" : "xy",
"destination" : "yx"
},
"paths" : [
[
"yy",
"tt",
"cc"
]
]
}`
预期输出:
Nodes : {"xx", "yy", "aa", "bb","xy", "yx"}
我尝试了 $setUnion
但它不起作用,因为我有字符串
$addToset
不可能添加两个字段:"origin" 和 "destination"
如何将我的集合字段(id.origin 和 id.destination)的所有值检索到一个集合中?
谢谢
在聚合管道中,首先您可以有两个集合(Originset 和 DestinationSet),然后您可以使用 setUnion 来获得这两个集合的并集。
由于这是您需要自定义格式的非常特殊的情况,因此最好的选择是 MongoDB 的 map-reduce 功能。然而,这种格式化也可以在聚合框架的帮助下实现。我正在添加这两种解决方案。
聚合框架:
db.collection.aggregate([
{
$group:{
_id:null,
origin:{
$addToSet:"$_id.origin"
},
destination:{
$addToSet:"$_id.destination"
}
}},
{
$project:{
_id:0,
Nodes:{
$setUnion:["$origin","$destination"]
}
}}
])
输出:
{
"Nodes" : [
"yy",
"yx",
"xx",
"bb",
"aa",
"xy"
]
}
地图缩小:
db.collection.mapReduce(
function () {
emit(1, this._id);
},
function (key, values) {
var o = {};
o.Nodes = [];
for (var i = 0; i < values.length; i++) {
o.Nodes.push(values[i].origin);
o.Nodes.push(values[i].destination);
}
return o;
},
{
out: { inline: 1 }
});
输出:
{
"results" : [
{
"_id" : NumberInt(1),
"value" : {
"Nodes" : [
"xx",
"yy",
"aa",
"bb",
"xy",
"yx"
]
}
}
],
"timeMillis" : NumberInt(22),
"counts" : {
"input" : NumberInt(3),
"emit" : NumberInt(3),
"reduce" : NumberInt(1),
"output" : NumberInt(1)
},
"ok" : NumberInt(1)
}
results.values.Nodes
包含您想要的结果。
我希望在一个集合中检索我的集合的所有值:
示例:
` "_id" : {
"origin" : "xx",
"destination" : "yy"
},
"paths" : [
[
"tt"
]
]
}
/* 2 */
{
"_id" : {
"origin" : "aa",
"destination" : "bb"
},
"paths" : [
[
"cc"
]
]
}
/* 3 */
{
"_id" : {
"origin" : "xy",
"destination" : "yx"
},
"paths" : [
[
"yy",
"tt",
"cc"
]
]
}`
预期输出:
Nodes : {"xx", "yy", "aa", "bb","xy", "yx"}
我尝试了 $setUnion
但它不起作用,因为我有字符串
$addToset
不可能添加两个字段:"origin" 和 "destination"
如何将我的集合字段(id.origin 和 id.destination)的所有值检索到一个集合中?
谢谢
在聚合管道中,首先您可以有两个集合(Originset 和 DestinationSet),然后您可以使用 setUnion 来获得这两个集合的并集。
由于这是您需要自定义格式的非常特殊的情况,因此最好的选择是 MongoDB 的 map-reduce 功能。然而,这种格式化也可以在聚合框架的帮助下实现。我正在添加这两种解决方案。
聚合框架:
db.collection.aggregate([
{
$group:{
_id:null,
origin:{
$addToSet:"$_id.origin"
},
destination:{
$addToSet:"$_id.destination"
}
}},
{
$project:{
_id:0,
Nodes:{
$setUnion:["$origin","$destination"]
}
}}
])
输出:
{
"Nodes" : [
"yy",
"yx",
"xx",
"bb",
"aa",
"xy"
]
}
地图缩小:
db.collection.mapReduce(
function () {
emit(1, this._id);
},
function (key, values) {
var o = {};
o.Nodes = [];
for (var i = 0; i < values.length; i++) {
o.Nodes.push(values[i].origin);
o.Nodes.push(values[i].destination);
}
return o;
},
{
out: { inline: 1 }
});
输出:
{
"results" : [
{
"_id" : NumberInt(1),
"value" : {
"Nodes" : [
"xx",
"yy",
"aa",
"bb",
"xy",
"yx"
]
}
}
],
"timeMillis" : NumberInt(22),
"counts" : {
"input" : NumberInt(3),
"emit" : NumberInt(3),
"reduce" : NumberInt(1),
"output" : NumberInt(1)
},
"ok" : NumberInt(1)
}
results.values.Nodes
包含您想要的结果。