In a $project
stage, I want to create a dummy (boolean) field which will be:
true
if some subfield existsfalse
otherwise
This is my data. The subfield of interest is address.country
, which may be missing:
{
"_id" : "1",
"name" : "John",
"address" : {
"city" : "New York",
"country" : "USA"
}
}
{
"_id" : "2",
"name" : "Paul",
"address" : {
"city" : "Paris"
}
}
The user named Paul has no address.country
subfield, so I want his dummy field to be false
.
Here is what I tried:
db.getCollection("items").aggregate(
[
{
"$match" : {
}
},
{
"$project": {
"name": true,
"has_country": { "$address.country": { "$exists": true } }
}
}
]
);
This failed with InvalidPipelineOperator
and message Unrecognized expression '$address.country'
. I found the question here but this use case is slightly different and the answer was not very understandable.