I have the following mongoclient query:
db = db.getSiblingDB("test-db");
hosts = db.getCollection("test-collection")
db.hosts.aggregate([
{$match: {"ip_str": {$in: ["52.217.105.116"]}}}
]);
Which outputs this:
{
"_id" : ObjectId("..."),
"ip_str" : "52.217.105.116",
"data" : [
{"ssl" : {"cert" : {"expired" : "False"}}}
]
}
I'm trying to build the query so it returns a boolean True
or False
depending on the value of the ssl.cert.expired
field. I'm not quite sure how to do this though. I've had a look into the $lookup
and $where
operators, but am not overly familiar with querying nested objects in Mongo yet.
CodePudding user response:
As the data
is an array, in order to get the (first) element of the nested expired
, you should work with $arrayElemAt
and provide an index as 0 to indicate the first element.
{
$project: {
ip_str: 1,
expired: {
$arrayElemAt: [
"$data.ssl.cert.expired",
0
]
}
}
}