Reputation: 49
I need to fetch the document from the db, data is as below
{
"systemName": "ABC",
"systemUsageAttrs" : [
{
"cpuUsage": 30,
"memUsage": 40,
"isActive": false
},
{
"cpuUsage": 88.2,
"memUsage": 33.5,
"isActive": false
}
]
},
{
"systemName": "DEF",
"systemUsageAttrs" : [
{
"cpuUsage": 30,
"memUsage": 40,
"isActive": false
},
{
"cpuUsage": 88.2,
"memUsage": 33.5,
"isActive": true
}
]
},
{
"systemName": "GHI",
"systemUsageAttrs" : [
{
"cpuUsage": 30,
"memUsage": 40,
"isActive": true
},
{
"cpuUsage": 88.2,
"memUsage": 33.5,
"isActive": true
}
]
}
I have used below piece of code, but it returns 2 documents instead of one.
List<Document> systemDetailsAL = sysUsageDetailsColl.aggregate(
asList(
unwind("$systemUsageAttrs"),
match(eq("systemUsageAttrs.isActive",false)),
group("$_id", Accumulators.first("systemName","$systemName"),
Accumulators.push("systemUsageAttrs", "$systemUsageAttrs")),
project(Projections.fields(Projections.excludeId()))
)
).into(new ArrayList<Document>());
Above code also provides the document which has isActive:false for one of the elements in the array.
Expected output is Document with systemName: ABC, since it has isActive:false in all the elements of array.
Any help/pointers appreciated.
Upvotes: 2
Views: 636
Reputation: 22974
db.collection.find({
"systemUsageAttrs": {
"$not": {
"$elemMatch": {
"isActive": {
$nin: [
false
]
}
}
}
}
})
$not
excludes documents which are identified by $elemMatch
where value is not false.
$elemMatch
identifies documents where isActive
not in false
$not
excludes that documents.You can convert this query to java compatible.
Problem with the code i.e query used:
unwind
Upvotes: 1