Hi,
I’m trying to find a way to dynamically use $lookup to get data from collections, without specifying the collection name, as it will be stored as a value of a field inside a document. To show you a little better, here’s my catalog.schema collection:
{
"_id": {
"$oid": "61e57f093ccf9d40b7ca270c"
},
"name": {
"label": "Name",
"dbfield": "name",
"active": true,
"hidden": false,
"fieldType": "text",
"source": null,
"order": {
"$numberInt": "1"
}
},
"category": {
"label": "Category",
"dbfield": "category",
"active": true,
"hidden": false,
"fieldType": "list",
"source": ["Cat1", "Cat2"],
"order": {
"$numberInt": "2"
}
},
"vendor": {
"label": "Supplier/Vendor",
"dbfield": "vendor",
"active": true,
"hidden": false,
"fieldType": "collection",
"source": {
"database": "catalog",
"collection": "vendors"
},
"order": {
"$numberInt": "3"
}
},
"manufacturer": {
"label": "Manufacturer",
"dbfield": "manufacturer",
"active": false,
"hidden": false,
"fieldType": "collection",
"source": {
"database": "catalog",
"collection": "manufacturers"
},
"order": {
"$numberInt": "4"
}
},
"_client": {
"$oid": "61e578f6907a8fc8d7dff877"
}
}
Also, the catalog.vendors:
{"_id":{"$oid":"61e7f52539926c421a53590e"},"_client":{{"$oid":"61e578f6907a8fc8d7dff877"},"name":"Amazon"}
{"_id":{"$oid":"61e7f5ff39926c421a535913"},"_client":{"$oid":"61e578f6907a8fc8d7dff877"},"name":"Ebay"}
And the catalog.manufacturers:
{"_id":{"$oid":"61e7f61339926c421a535914"},"_client":{"$oid":"61e578f6907a8fc8d7dff877"},"name":"Other"}
{"_id":{"$oid":"61e7f64e39926c421a535915"},"_client":{"$oid":"61e578f6907a8fc8d7dff877"},"name":"RockAuto"}
Here’s the query that I have so far:
[{
$match: {
"_client": {
"$oid": "61e578f6907a8fc8d7dff877"
}
}
}, {
$project: {
'_id': 0,
'data': {
'$objectToArray': {
'$arrayToObject': {
'$filter': {
'input': {
'$objectToArray': '$$ROOT'
},
'cond': {
'$not': [{
'$regexMatch': {
'input': '$$this.k',
'regex': '^_',
'options': 'm'
}
}]
}
}
}
}
}
}
}, {
$unwind: {
path: "$data"
}
}, {
$lookup: {
'from': 'data.v.source.collection',
'pipeline': [],
'as': 'data.v.collection'
}
}, {
$group: {
'_id': null,
'wind': {
'$addToSet': '$data'
}
}
}, {
$replaceRoot: {
'newRoot': {
'$arrayToObject': '$wind'
}
}
}]
The problem I’m facing is that $lookup does not accepts or recognizes the value of the field that I’m referencing as the ‘from’ collection source. Any ideas on how to get around this?