如何在2个集合MongoDb中显示id为但数据为的名称?
我只想通过相应的名称更改Id。但是id在一个集合中,而name在另一个集合中。“$lookup”Mongo在我的情况下不起作用 第一个集合“参数”包含具有“类别id”的项: 第二个集合“参考”包含具有\u id(与第一个集合中的类别id相同)和名称的类别描述: 我想要类似的东西(带有类别名称而不是id的项目): 我的问题是: 但结果始终是id而不是名称:如何在2个集合MongoDb中显示id为但数据为的名称?,mongodb,Mongodb,我只想通过相应的名称更改Id。但是id在一个集合中,而name在另一个集合中。“$lookup”Mongo在我的情况下不起作用 第一个集合“参数”包含具有“类别id”的项: 第二个集合“参考”包含具有\u id(与第一个集合中的类别id相同)和名称的类别描述: 我想要类似的东西(带有类别名称而不是id的项目): 我的问题是: 但结果始终是id而不是名称: { "_id" : ObjectId("56cc8827b9e4ed0fd42a4569"), "type" : "lau
{
"_id" : ObjectId("56cc8827b9e4ed0fd42a4569"),
"type" : "launcher",
"categories" : [
"5964961294ff4a37988e8f9b", //--->> id not name from reference collection !
"596495c994ff4a37988e8f99", //--->> id not name from reference collection !
...
如何使用类别名称而不是id。
Sql非常简单(结合外键和引用表),但Mongo查询非常复杂
谢谢你的帮助
我是你的朋友 您的方法有几个问题:
ObjectId
和字符串
值之间调用$lookup
类别
字段,您正在使用数据中的类别id
映射它。类别
,而它应该来自引用。名称
李>
category\u id
嵌套在一个数组中,当您调用$lookup
时,您将它与整个数组匹配,而不是与键
字段匹配category\u id
转换为ObjectId
,然后为该字段调用$lookup
(而不是修改实际字段,我添加了一个名为cat\u id
的新字段)
我会这样做:
db.parameter.aggregate([
{
$match: {
type: { $in: ["launcher"] },
}
},
{ $unwind: "$data.categories" },
{ $addFields: { "data.categories.cat_id": { $convert: { input: "$data.categories.category_id", to: "objectId" } } } },
{
$lookup: {
from: "references",
localField: "data.categories.cat_id",
foreignField: "_id",
as: "ref"
}
},
{
$project:
{
_id: 1, type: 1, categories: "$ref.name"
}
}
]);
这将为您提供以下输出:
{
"_id" : ObjectId("56cc8827b9e4ed0fd42a4569"),
"type" : "launcher",
"categories" : [
"tsSupportNormal01"
]
},
{
"_id" : ObjectId("56cc8827b9e4ed0fd42a4569"),
"type" : "launcher",
"categories" : [
"**qcSupportNormal01**"
]
},
{
"_id" : ObjectId("56cc8827b9e4ed0fd42a4847"),
"type" : "launcher",
"categories" : [
"**qcSupportNormal01**"
]
},
{
"_id" : ObjectId("56cc8827b9e4ed0fd42a4847"),
"type" : "launcher",
"categories" : [ ]
}
请在jsoneditorThank与您共享这两个集合和您想要的集合,您可以使用Viksool,但我使用的是Mongo 3.4,$convert自Mongo 4.0以来就存在。我正在寻找3.4中的转换字符串对象。@caloux我建议将MongoDB升级到最新版本或至少是v4.0版本(这样,您可以使用更有用的功能)。如果不可能,您可能需要编写一个脚本来执行类似于1的输出流。使用
字符串中的id
创建所有类别的名称的数组,然后使用过滤器='launcher'
从参数中创建另一个数组,然后在参数对象上执行foreach循环,并从类别数组中附加匹配值。@Viksool感谢您的帮助。@Viksool反馈,我将更新到v4.0,更好地使用有用的功能。
db.parameters.aggregate([
///////////////////////item filter
{$match: {
type:{ $in: [ "launcher" ] } ,
}},
///////////////////// foreign field
{$lookup: {
from: "references",
localField: "categories",
foreignField: "_id",
as: "references"
}},
/////////////////// projection
{$project:
{_id:1,type:1,categories:"$data.categories.category_id"
}},
])
{
"_id" : ObjectId("56cc8827b9e4ed0fd42a4569"),
"type" : "launcher",
"categories" : [
"5964961294ff4a37988e8f9b", //--->> id not name from reference collection !
"596495c994ff4a37988e8f99", //--->> id not name from reference collection !
...
db.parameter.aggregate([
{
$match: {
type: { $in: ["launcher"] },
}
},
{ $unwind: "$data.categories" },
{ $addFields: { "data.categories.cat_id": { $convert: { input: "$data.categories.category_id", to: "objectId" } } } },
{
$lookup: {
from: "references",
localField: "data.categories.cat_id",
foreignField: "_id",
as: "ref"
}
},
{
$project:
{
_id: 1, type: 1, categories: "$ref.name"
}
}
]);
{
"_id" : ObjectId("56cc8827b9e4ed0fd42a4569"),
"type" : "launcher",
"categories" : [
"tsSupportNormal01"
]
},
{
"_id" : ObjectId("56cc8827b9e4ed0fd42a4569"),
"type" : "launcher",
"categories" : [
"**qcSupportNormal01**"
]
},
{
"_id" : ObjectId("56cc8827b9e4ed0fd42a4847"),
"type" : "launcher",
"categories" : [
"**qcSupportNormal01**"
]
},
{
"_id" : ObjectId("56cc8827b9e4ed0fd42a4847"),
"type" : "launcher",
"categories" : [ ]
}