我在下面有一個“影像”集合(注意:標簽欄位是一個陣列,因為 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 條記錄相同的“標簽”欄位?請幫助我,謝謝
uj5u.com熱心網友回復:
該$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"]
},
像這個例子:操場1 這是基于@turivishal的這個答案
如您所說,如果您想將它們放入新陣列中,則可以添加:
{
$addFields: {
labelDocuments: {
$map: {
input: "$labels",
as: "i",
in: {
$mergeObjects: [
{},
{
$first: {
$filter: {
input: "$labelDocuments",
cond: {
$eq: [
"$$this._id",
"$$i.labelId"
]
}
}
}
}
]
}
}
}
}
},
游樂場 2。
如果您想將標簽保留在影像檔案下,最好不$unwind要這樣做,因為這將是一項代價高昂的冗余操作,但正如我所說,這取決于您接下來要做什么。
uj5u.com熱心網友回復:
你應該先放松一下。
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
}
}
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/476005.html
標籤:节点.js mongodb 猫鼬 mongodb查询 聚合框架
