Collation is not a function

In the Mongo shell this works correctly to create a case-insensitive search of a database for which I have created a case-insensitive index:

use dt3_api
db["LCP Production"].find({dt_name:"kaplan"}).collation({locale: 'en_US', strength: 1})

I am trying to do the same thing as a Realm function as follows:

exports= function (payload) {
  
  // const query={"dt_name":(payload.query.testparam)};
  const mongodb=context.services.get("mongodb-atlas");
  const mycollection = mongodb.db("dt3_api").collection("LCP Production");
 
 return mycollection.find({dt_name:"kaplan"}).collation({locale: 'en_US', strength: 1})

}; 

But when I run this I get this error:

image

I am using Mongo 4.4.8 so I believe the collation function should work (as indeed it works in the shell).

Is there some alternate way to run a search on a collated index as a Realm function?

1 Like

Later - does this mean there is no way to use a collated index with a Realm function?

A workaround seems to be too use a text index search -this does work - however it searches globally in all fields which have a text index. Is there a way to restrict this to only specific fields?

return mycollection.find ( {$text: { $search: payload.query.testparam, $caseSensitive:false}})

i had this same error with collation except i was trying to use aggregate to sort text alphabetically (mongodb 4.4)
my remedy:

{
  '$addFields': {
    'sort_title': {
      '$toLower': '$title_data'
    }
  }
},
{
  '$sort': {
     'sort_title': 1
   }
}

use $addFields to add a new lowercase field of the target field’s text, then sort by the new lowercase field

I was surprise not to find a solution here. It’s so weird that it works on mongodb shell but not in Realm.

They need to fix this

Also, I tried @Richard_Kaplan solution and it works great if I write findOne instead of find (which doesn’t work)