Reputation: 457
Input Documents
"data": {
"abc": {
"Id": "100"
},
"xyz": {
"Id": "123"
}
}
Explanation :
I want to do the $match
on data.{i} i is parameter
if I give to "abc" as a parameter
I get the following output, I want to pass the multiple parameters to i
"abc", "xyz"..
How I can do that in do that $match the object key using parameter.
Expected Output :
"data": {
"abc": {
"Id": "100"
},
}
Upvotes: 1
Views: 482
Reputation: 4452
Try this:
let keys = ["abc"];
db.collectionName.aggregate([
{
$project: {
"filteredData": {
$filter: {
input: { $objectToArray: "$data" },
as: "item",
cond: {
$in: ["$$item.k", keys]
}
}
}
}
},
{
$project: {
data: { $arrayToObject: "$filteredData" }
}
}
]);
Output: When array keys = ["abc"];
{
"_id" : ObjectId("60395e36e0c7d52970d3fa21"),
"data" : {
"abc" : {
"Id" : "100"
}
}
}
Output: When array keys = ["abc", "xyz"];
{
"_id" : ObjectId("60395e36e0c7d52970d3fa21"),
"data" : {
"abc" : {
"Id" : "100"
},
"xyz" : {
"Id" : "123"
}
}
}
Upvotes: 1
Reputation: 36104
$exists
let i = "abc";
Schema.find(
{ ["data."+i]: { $exists: true } },
{ ["data."+i]: 1 }
)
Second option if you have list of keys in array,
$or
condition, and prepare project partlet i = ["abc", "xyz"];
let query = [], project = {};
i.map(k => {
query.push({ ["data."+k]: { $exists: true } });
project["data."+k] = 1;
});
Schema.find({ $or: query }, project);
Third option using project operators starting from MongoDb v4.4, with more dynamic approach,
$objectToArray
convert object to array$filter
to filter above converted array and get matching elements$arrayToObject
convert array back to objectlet i = "abc";
Schema.find(
{ ["data."+i]: { $exists: true } },
{
data: {
$arrayToObject: {
$filter: {
input: { $objectToArray: "$data" },
cond: { $eq: ["$$this.k", i] }
}
}
}
}
)
Upvotes: 2