Search code examples
mongodbrealmfull-text-searchserverless

MongoDB - Realm (aggregation $search text) needs a role with search document-level permission to perform a $search


I'm trying to do a full-text query to my MongoDB using Realm as my Serverless Authenticator and functions-Server. In Realm, I made a function that can be called from the client to do this query. The function looks like this:

exports = function( jobName,collection="jobs"){
  const agg = [
   {
    '$search': {
      'text': {
        'path': [
          'name', 'description'
        ],
        'query': jobName,
        'fuzzy': {}
       }
     }
   }
   ];
   var collectionObj = context.services.get("mongodb-atlas").db("myDb").collection(collection);
   var result = collectionObj.aggregate(agg)
   return result;
};

If I run this function as a System User I get exactly what I want from the DB. But if I want to run the search as an Anonymous User I get the following error:

A role with a search document-level permission is required to perform a $search

So I went to my roles and I created a role called normal with document-level search permissions: User

The error is still happening and I have no clue where to keep looking. The Advanced Options in the User also show the search field turned to true:

  {
  "name": "normal",
  "apply_when": {},
  "insert": false,
  "delete": false,
  "search": true,
  "read": true,
  "fields": {
    "description": {},
    "name": {}
  },
  "additional_fields": {}
}

Solution

  • So there is a way to use the full-text function through realm from a Client but it involves creating an open REST API. Instead of calling a function through the client we can create a REST API and return the search from that entry point. Karen Huaulme from MongoDB did something like that on her Movie Tutorial. This is really not the Answer I was hoping for but it is a nice workaround.