meteor / meteor-feature-requests

A tracker for Meteor issues that are requests for new functionality, not bugs.
Other
89 stars 3 forks source link

Support the $search operator for MongoDB aggregation pipelines (for Atlas) #414

Closed HemalR closed 3 years ago

HemalR commented 3 years ago

Mongo Atlas release a native search feature that allows devs to add search to their applications (very quickly). Unfortunately, from what I can see, it is not yet supported by Meteor's Mongo driver.

The below is from my comment in the forums:

---javascript

Trying to experiment with it to replace some in-code search processes we have that are getting a bit slow as our collection grows.

For some reason though, I keep getting an error:

MongoError: Unrecognized pipeline stage name: '$search'

I tested this out by doing the following:

  1. Creating a cluster with a collection of sample data and the relevant search indexes (using the Atlas GUI)
  2. Running the below code on a non-Meteor node app to ensure it is working okay:
const MongoClient = require('mongodb').MongoClient;
const uri = <MongoURL>
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(async (err) => {
    try {
        const res = await client
            .db('meteor')
            .collection('notifications')
            .aggregate([
                {
                    $search: {
                        text: {
                            query: 'event',
                            path: 'title',
                        },
                    },
                },
            ])
            .toArray();
        console.log(res); // Logs a list of search result documents
        console.log(`${res.length} results`);
    } catch (error) {
        console.error(error);
    }
    client.close();
});
  1. Running the same code within Meteor (getting the error described) - and I know that the aggregate pipeline works with other operators because I tested out $match:
try {
        const res = await Notifications.rawCollection()
            .aggregate([
                {
                    $search: {
                        text: {
                            query: 'event',
                            path: 'title',
                        },
                    },
                },
            ])
            .toArray();
        console.log(res);
    } catch (err) {
        console.error(err); // MongoError: Unrecognized pipeline stage name: '$search'
    }
};

I think this would be a useful feature to have and that the proportion of Meteor devs who use Atlas is high.

Thanks in advance!

HemalR commented 3 years ago

Update - This is in fact supported and was an error on my end.

Apologies for the needless issue!