I have a query implemented in the below link wherein I am trying to get all the depots based on the city
values present in charge_level_key
inside contracts
. In other words, each string value in charge_level_key
is a city that has to be matched with the depots and the corresponding depots should be fetched.
The current implementation is working with case sensitive. I need to make it work as the same way with case insensitivity. How would I do that
https://mongoplayground.net/p/ZH6K5gkJxrs
Thanks in advance
CodePudding user response:
With regular $lookup
you can use a pipeline with $toLower
:
db.contracts.aggregate([
{$match: {_id: ObjectId("630c9e23fb146c4b3b801b9e")}},
{$set: {
charge_level_key: {$map: {input: "$charge_level_key", in: {"$toLower": "$$this"}}}
}},
{
$lookup: {
from: "depots",
let: {charge_level_key: "$charge_level_key"},
pipeline: [
{$match: {
$expr: {
$in: [{$toLower: "$city"}, "$$charge_level_key"]
}
}
}
],
as: "depots"
}
},
{$sort: {count: -1}}
])
See how it works on the playground example