[英]mongodb - How to access lookup results in another lookup in aggregation?
[英]How to use aggregation/ lookup in mongoDB with multi results?
我在下面有一个“图像”集合(注意:标签字段是一个数组,因为 1 个图像可能有很多标签)
{
"_id" : ObjectId("624d182b6a4e7f001ed5cd9f"),
"is_deleted" : false,
"labels" : [
{
"_id" : ObjectId("627a0fbc12458800209dd69c"),
"labelId" : ObjectId("624d14d76a4e7f001ed5c6ca"),
"xmin" : 0.440625,
"xmax" : 0.2953125,
"ymin" : 0.102083333333333,
"ymax" : 0.366666666666667
},
{
"_id" : ObjectId("627a0fbc12458800209dd69d"),
"labelId" : ObjectId("624d14d76a4e7f001ed5c6ca"),
"xmin" : 0.2546875,
"xmax" : 0.45,
"ymin" : 0.220833333333333,
"ymax" : 0.439583333333333
},
{
"_id" : ObjectId("627a0fbc12458800209dd69e"),
"labelId" : ObjectId("624d14d76a4e7f001ed5c6ca"),
"xmin" : 0.240625,
"xmax" : 0.48125,
"ymin" : 0.208333333333333,
"ymax" : 0.466666666666667
},
{
"_id" : ObjectId("627a0fbc12458800209dd69f"),
"labelId" : ObjectId("624d14d76a4e7f001ed5c6ca"),
"xmin" : 0.275618374558304,
"xmax" : 0.436395759717314,
"ymin" : 0.136470588235294,
"ymax" : 0.404705882352941
}
],
"img_name" : "6917328478506479617.jpg",
"img_originalname" : "frame27406302021-085850.jpg",
"img_desc" : "Cyber Sercurity multi upload",
"img_uri" : "http://localhost:8080/resources/images/2022/4/6/6917328478506479617.jpg",
"img_path" : "/resources/images/2022/4/6/6917328478506479617.jpg",
"datasetId" : ObjectId("624d14216a4e7f001ed5c459"),
"createdAt" : ISODate("2022-04-06T04:33:47.120Z"),
"updatedAt" : ISODate("2022-05-10T07:09:48.306Z"),
"__v" : 0
}
下面是“标签”集合:
{
"_id" : ObjectId("624d14d76a4e7f001ed5c6ca"),
"is_deleted" : false,
"labelName" : "Đông",
"description" : "qw",
"datasetId" : ObjectId("624d14216a4e7f001ed5c459"),
"createdAt" : ISODate("2022-04-06T04:19:35.985Z"),
"updatedAt" : ISODate("2022-04-06T04:19:35.985Z"),
"__v" : 0,
"datanoiseId" : ObjectId("6261318b23a7a590480f92e6")
}
现在我想查找将有关标签的所有信息加入到图像中,所以我这样做了:
db.Image.aggregate([
{
$match: {
$and: [{"datasetId": ObjectId("624d14216a4e7f001ed5c459"), "is_deleted": false}]
}
},
{
$lookup: {
from: "Label",
localField: "labels.labelId",
foreignField: "_id",
as: "labelDocuments",
},
},
{ $sort : { "_id" : -1 } }
])
在执行以下查询后得到的结果:
{
"_id" : ObjectId("624d182b6a4e7f001ed5cd9f"),
"is_deleted" : false,
"labels" : [
{
"_id" : ObjectId("627a0fbc12458800209dd69c"),
"labelId" : ObjectId("624d14d76a4e7f001ed5c6ca"),
"xmin" : 0.440625,
"xmax" : 0.2953125,
"ymin" : 0.102083333333333,
"ymax" : 0.366666666666667
},
{
"_id" : ObjectId("627a0fbc12458800209dd69d"),
"labelId" : ObjectId("624d14d76a4e7f001ed5c6ca"),
"xmin" : 0.2546875,
"xmax" : 0.45,
"ymin" : 0.220833333333333,
"ymax" : 0.439583333333333
},
{
"_id" : ObjectId("627a0fbc12458800209dd69e"),
"labelId" : ObjectId("624d14d76a4e7f001ed5c6ca"),
"xmin" : 0.240625,
"xmax" : 0.48125,
"ymin" : 0.208333333333333,
"ymax" : 0.466666666666667
},
{
"_id" : ObjectId("627a0fbc12458800209dd69f"),
"labelId" : ObjectId("624d14d76a4e7f001ed5c6ca"),
"xmin" : 0.275618374558304,
"xmax" : 0.436395759717314,
"ymin" : 0.136470588235294,
"ymax" : 0.404705882352941
}
],
"img_name" : "6917328478506479617.jpg",
"img_originalname" : "frame27406302021-085850.jpg",
"img_desc" : "Cyber Sercurity multi upload",
"img_uri" : "http://localhost:8080/resources/images/2022/4/6/6917328478506479617.jpg",
"img_path" : "/resources/images/2022/4/6/6917328478506479617.jpg",
"datasetId" : ObjectId("624d14216a4e7f001ed5c459"),
"createdAt" : ISODate("2022-04-06T04:33:47.120Z"),
"updatedAt" : ISODate("2022-05-10T07:09:48.306Z"),
"__v" : 0,
"labelDocuments" : [
{
"_id" : ObjectId("624d14d76a4e7f001ed5c6ca"),
"is_deleted" : false,
"labelName" : "Đông",
"description" : "qw",
"datasetId" : ObjectId("624d14216a4e7f001ed5c459"),
"createdAt" : ISODate("2022-04-06T04:19:35.985Z"),
"updatedAt" : ISODate("2022-04-06T04:19:35.985Z"),
"__v" : 0,
"datanoiseId" : ObjectId("6261318b23a7a590480f92e6")
}
]
}
你看,字段“labels”有 4 条记录,但在“labelDocuments”中只有 1 条记录? 原因是在给图片分配标签的时候,我给1张图片分配了很多标签(labelId相同),所以lookup命令只能加入1条记录? 那么我怎样才能让“labelDocuments 在这种情况下有 4 条记录相同的“标签”字段?请帮助我,谢谢
$lookup
操作可以获取多条记录。 在您的数据中,您只有一个( ObjectId("624d14d76a4e7f001ed5c6ca")
),您的所有标签都指的是它。 这就是您在labelDocuments
数组中只获得一个文档的原因。
如果您的目标是合并标签,则可以将查询添加一个步骤:
{
$addFields: {
mergedLabels: {
$map: {
input: "$labels",
as: "i",
in: {
$mergeObjects: [
"$$i",
{
$first: {
$filter: {
input: "$labelDocuments",
cond: {
$eq: [
"$$this._id",
"$$i.labelId"
]
}
}
}
}
]
}
}
}
}
},
{
$unset: ["labelDocuments", "labels"]
},
如您所说,如果您想将它们放入新数组中,则可以添加:
{
$addFields: {
labelDocuments: {
$map: {
input: "$labels",
as: "i",
in: {
$mergeObjects: [
{},
{
$first: {
$filter: {
input: "$labelDocuments",
cond: {
$eq: [
"$$this._id",
"$$i.labelId"
]
}
}
}
}
]
}
}
}
}
},
游乐场 2 。
如果您想将标签保留在图像文档下,最好不要$unwind
,因为这将是一项代价高昂的冗余操作,但正如我所说,这取决于您接下来要做什么。
你应该先放松一下。
db.Image.aggregate([{ $match: { $and: [{ "datasetId": ObjectId("624d14216a4e7f001ed5c459"), "is_deleted": false }] } }, { $unwind: "$lables" }, { $lookup: { from: "Label", localField: "labels.labelId", foreignField: "_id", as: "labelDocuments", }, }, { $sort: { "_id": -1 } } ])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.