I have a collection in Mongo DB which looks like this -
const clientsColection = {
"_id" : ObjectId("5ec8492c27ecdc17362b86cb"),
"clientName" : "data" ,
"users" : [
{
"roles" : [],
"operations" : [],
"_id" : ObjectId("5ecac60ab527bd0ba4a615cf"),
"isAdmin" : false,
"username" : "Adduser"
},
{
"roles" : [],
"operations" : [],
"_id" : ObjectId("5ecac60ab527bd0ba4a616cf"),
"isAdmin" : false,
"username" : "new"
}
],
"kpiObj" : [
{
"kpiName" : "epsilon",
"resultObj" : {
"result" : [
{
"mark" : 4,
"plz" : "01069"
},
{
"mark" : 5,
"plz" : "01067"
}
],
}
},
{
"kpiName" : "epsilon2",
"resultObj" : {
"result" : [
{
"mark" : 3,
"plz" : "01069"
},
{
"mark" : 1,
"plz" : "01067"
}
],
}
}
]
}
I am trying to perform a filter on a nested array of objects using aggregate, project, filter operators but I have not been successful yet in getting the expected output.
I want to achieve the following:-
- stage 1: matching the
usersarray object forusers.usernameand retrieving the matching array of objects (only 1 always). - stage 2: match the output from stage 1 with
kpiNameinkpiObjand retrieve the matching array of objects (only one always). - stage 3. match the output from stage 2 with
markinresultObjand retrieve the matching array of objects.
I've spent the last 3 days watching several tutorials and going through several stack overflow questions but hadn't been able to get the expected output. I've been able to get the expected output for stage1 by using following query. Any help would be greatly appreciated.
db.getCollection("clientsCollection").aggregate([
{ $match: { 'users.username': 'Adduser' } },
{
$project: {
users: {
$filter: {
input: '$users',
as: 'user',
cond: { $eq: ['$$user.username', 'Adduser'] }
}
}, 'kpiObj.kpiName':1, 'kpiObj.resultObj.result.score':1 , 'kpiObj.resultObj.result.plz':1
}
}
])
OUTPUT*
for matching username as Adduser, kpiObj.kpiName as epsilon and kpiObj.resultObj.result.mark as 4, I am expecting following output :-
const clientsColection = {
"_id" : ObjectId("5ec8492c27ecdc17362b86cb"),
"clientName" : "data" ,
"users" : [
{
"username" : "Adduser"
}
],
"kpiObj" : [
{
"kpiName" : "epsilon",
"resultObj" : {
"result" : [
{
"mark" : 4,
"plz" : "01069"
}
],
}
}
]
}
kpiName? What are you trying to get in terms of output documents?