Hi,
I’m trying to do a case-insensitive query to lookup for email.
I cannot add an index with a collation and I do not want to use regex for this.
I tried to use collation as an option directly inside the query. I does work when I try on mongosh inside Compass but when I try to implement it inside my function it doesn’t work.
How I reproduce :
Imagine a collection named ‘Player’ with inside this document :
{
_id: 'primary_key',
accountId: 'random-id-i-use',
email: 'anEmail'
}
Here the atlas’s function code I use to test where I try different syntax hoping it will work :
// exports({email: 'anEmail', accountId: 'random-id-i-use'}) => work since same case
// exports({email: 'anemail', accountId: 'random-id-i-use'}) => doesn't work
exports = async function (payload) {
const queryOptions = {
collation: {
locale: 'en',
strength: 1
}
};
const { email, accountId } = payload;
let collection = context.services.get("mongodb-atlas").db("padel-app-db").collection("Player");
let count1 = await collection.count({ accountId: accountId, email: email },);
let count2 = await collection.count({ accountId: accountId, email: email }, queryOptions);
let player1 = await collection.findOne({ accountId: accountId, email: email });
let player2 = await collection.findOne({ accountId: accountId, email: email }, queryOptions);
let player3 = await collection.findOne({ accountId: accountId, email: email }, {}, queryOptions);
let log = {
isSystemUser: context.runningAsSystem(), // return true
count1,
count2,
player1: player1 == undefined ? null : player1._id,
player2: player2 == undefined ? null : player2._id,
player3: player3 == undefined ? null : player3._id,
}
return log;
};
I didn’t find any mention of how to use collation
option here : https://www.mongodb.com/docs/atlas/app-services/functions/mongodb/api/#collection.findone
But here https://www.mongodb.com/docs/atlas/app-services/mongodb/crud-and-aggregation-apis/#query-options I saw this :
All query options are available in system functions.
Can anyone help me with this please ?
Thanks !