My documents are structured similarly to this:
{
primaryToken: 123,
settings: {
tokenList: [
{
number: 987,
status: ACTIVE
},
{
number: 123,
status: INACTIVE
}
]
}
}
How can I query for documents who have a token in the tokenList that matches the primaryToken and has in a status of ‘INACTIVE’.
I have tried the following, but it does not work:
{
settings.tokenList : {
$elemMatch : {
number : $primaryToken,
status : INACTIVE"
}
}
}
For anyone else seeking a solution with a similar challenge - here is how I eventually managed to achieve the desired result:
{
"settings.tokenList": { $exists: true, $ne: null },
"primaryToken": { $exists: true, $ne: null },
$expr: {
$anyElementTrue: {
$map: {
input: "$settings.tokenList",
as: "token",
in: {
$and: [
{ $eq: ["$$token.number", "$primaryToken"] },
{ $eq: ["$$token.status", "INACTIVE"] }
]
}
}
}
}
}