I need to lookup data from other collection, I have done this before but when trying to get ot subarray, it is turning painful and I am not sure what I am doing wrong.
Below is a sample from values collection.
{
"_id" : "328925Atuador cerâmico, símbolo X",
"valueName" : "Atuador cerâmico, símbolo X",
"valueDescription" : "",
"language" : "Portuguese",
"changedOn" : "2/2/2021 5:36 PM",
"valueAlias" : "",
"vid" : "328925",
"createdOn" : "9/18/2019 10:53 AM",
"timestamp" : ISODate("2023-01-26T18:08:20.118+0000")
}
{
"_id" : "328925แกนเซรามิก",
"valueName" : "แกนเซรามิก",
"valueDescription" : "",
"language" : "Thai",
"changedOn" : "",
"valueAlias" : "",
"vid" : "328925",
"createdOn" : "9/18/2019 10:53 AM",
"timestamp" : ISODate("2023-01-26T18:08:20.118+0000")
}
sample document from backbone collection
{
"_id" : "164",
"structureGroupIdentifier" : "164",
"structureGroupParentIdentifier" : "11",
"structureGroupName" : "Power Transformers",
"features" : [
{
"pid" : "69",
"parameterName" : "Mounting Type (69)",
"vid" : [
"1",
" 328925",
" 339014",
" 384993",
" 409393",
" 411897",
" 420487"
],
"rank" : "375",
"priority" : "Filterable",
"isRange" : "",
"dataType" : "Character string",
"multivalue" : "No",
"leadingParameterIdentifier" : "",
"followingParameterIdentifier" : "",
"createdOn" : "9/17/2019 4:06 PM",
"changedOn" : "7/20/2020 12:30 PM",
"longTailKeyword" : ""
},
],
"timestamp" : ISODate("2023-01-31T01:31:05.787+0000")
}
I developed a pipeline to merge all languages from values collection under a vid
[
{
"$match" : {
"vid" : "328925"
}
},
{
"$group" : {
"_id" : "$vid",
"vidLanguages" : {
"$push" : "$$CURRENT"
}
}
}
]
I used this pipeline for a lookup in the backbone collection, I am confused about using $$CURRENT in the pipeline of a lookup, but I am not sure of how else accomplish what I need.
Here is the pipeline.
[
{
"$match" : {
"structureGroupIdentifier" : "164"
}
},
{
"$unwind" : {
"path" : "$features",
"preserveNullAndEmptyArrays" : true
}
},
{
"$lookup" : {
"from" : "values_master_collection",
"let" : {
"backbone_vid" : "$features.vid"
},
"pipeline" : [
{
"$match" : {
"vid" : "$$backbone_vid"
}
},
{
"$group" : {
"_id" : "$vid",
"vidLanguages" : {
"$push" : "$$CURRENT"
}
}
}
],
"as" : "feature.vid"
}
}
]
what I am getting is
{
"_id" : "164",
"structureGroupIdentifier" : "164",
"structureGroupParentIdentifier" : "11",
"structureGroupName" : "Power Transformers",
"features" : {
"pid" : "69",
"parameterName" : "Mounting Type (69)",
"vid" : [
],
"rank" : "375",
"priority" : "Filterable",
"isRange" : "",
"dataType" : "Character string",
"multivalue" : "No",
"leadingParameterIdentifier" : "",
"followingParameterIdentifier" : "",
"createdOn" : "9/17/2019 4:06 PM",
"changedOn" : "7/20/2020 12:30 PM",
"longTailKeyword" : ""
},
"timestamp" : ISODate("2023-01-31T01:31:05.787+0000")
}
what I need is for it look like this
{
"_id" : "164",
"structureGroupIdentifier" : "164",
"structureGroupParentIdentifier" : "11",
"structureGroupName" : "Power Transformers",
"features" : {
"pid" : "69",
"parameterName" : "Mounting Type (69)",
"vid" : [
{
"_id" : "328925",
"vidLanguages" : [
{
"_id" : "328925Atuador cerâmico, símbolo X",
"valueName" : "Atuador cerâmico, símbolo X",
"valueDescription" : "",
"language" : "Portuguese",
"changedOn" : "2/2/2021 5:36 PM",
"valueAlias" : "",
"vid" : "328925",
"createdOn" : "9/18/2019 10:53 AM",
"timestamp" : ISODate("2023-01-26T18:08:20.118+0000")
},
{
"_id" : "328925แกนเซรามิก",
"valueName" : "แกนเซรามิก",
"valueDescription" : "",
"language" : "Thai",
"changedOn" : "",
"valueAlias" : "",
"vid" : "328925",
"createdOn" : "9/18/2019 10:53 AM",
"timestamp" : ISODate("2023-01-26T18:08:20.118+0000")
},
]
},
{}//many more objects
],
"rank" : "375",
"priority" : "Filterable",
"isRange" : "",
"dataType" : "Character string",
"multivalue" : "No",
"leadingParameterIdentifier" : "",
"followingParameterIdentifier" : "",
"createdOn" : "9/17/2019 4:06 PM",
"changedOn" : "7/20/2020 12:30 PM",
"longTailKeyword" : ""
},
"timestamp" : ISODate("2023-01-31T01:31:05.787+0000")
}
all help is appreciated