Text search only works as a system user

I have the following code to do a text search on a field:

const offerersSearchResolver = async (input) => {
    const db = context.services.get('mongodb-atlas').db('xxx');
    const users = db.collection('xxx');
    const skills = input.skills || [];
    return (await users
        .aggregate([
          {$match: {
                $text: { $search: input.searchTerm },
            },},
        { $sort: { score: { $meta: 'textScore' }, 'fullName.last': 1, 'fullName.first': 1 } }
    ])
        .toArray());
};
exports = offerersSearchResolver;

It only works as a system user.
If I select “Application Authentication” I get (Location17313) $match with $text is only allowed as the first pipeline stage.

Please help, I can’t run this query as a system user!

edit: I can even imagine how it happens: Realm probably inserts some conditions to enforce privileges and it upsets $text.

I just realised that a resolver’s response is being filtered according to rules even when it’s running as System, so it solves the problem for me!
Though would have been nice to update the docs and mention this problem.

Are you using Atlas for this query?

@Marcus, considering his service named mongodb-atlas i am pretty sure he is.

They should definitely consider Atlas Search as you probably wanted to advise

https://docs.atlas.mongodb.com/atlas-search/

1 Like

Dimitri, you should be using Atlas Search instead of $match & $text.

One thing that you eliminate here is you don’t have to sort by text score, as results are sorted by relevance by default.

Your relevance will also be improved. Take a look and let me know if you have any questions: https://docs.atlas.mongodb.com/reference/atlas-search/query-syntax

1 Like

I apologise for slow response, missed the notifications.
Thanks for the hint, I’ll look into Atlas Search!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.