I have a User collection with the following structure.
{
_id:5aeb04db3a336840e2a7fde8
name:"John Doe"
email:"john@gmail.com",
login_activity: [
{
platform: "Windows,
browser: "Chrome",
browser_version: "95.0.232"
time: 2021-05-22T00:00:00.000+00:00
},
{
time: 2016-06-23T00:00:00.000+00:00
},
{
platform: "Linux,
browser: "Firefox",
browser_version: "94.0.232"
time: 2021-06-24T00:00:00.000+00:00
},
{
platform: "Linux,
browser: "Firefox",
browser_version: "94.0.232"
time: 2021-06-25T00:00:00.000+00:00
},
]
}
I am trying to get a break up of the platform i.e Windows: 1, Linux: 2 etc. For that, I first need to filter out entries that do not have a platform attribute. I have the following $filter stage which does not seem to be working as expected.
{
$set: {
filtered_login_activities: {
$filter: {
input: "$login_activity",
as: "login_session",
cond: {
$ne: ["$$login_session.platform", null]
}
}
}
}
}
But for some reason, it is not filtering out the documents with the platform attribute not present. What am I doing wrong?