I want to do a text search in MongoDB. In development I use a local mongodb, and in production I use MongoDB Atlas. I want to do a $search
Atlas Search if the db I am connected to is MongoDB Atlas. If I'm connected to a local db I want to use $text
.
If it ends with .mongodb.net
, use Atlas Search. If not, use normal text search.
In production it won't be slow because first it will attempt $search
and it will work in production. In development it might be slow because of making an extra failing query every time.
Is there a way of mimicking the Atlas search behavior for a local MongoDB? It's okay if it involves a really slow query but it would be nice to test with same quality text matching as there will be in production.
I would say both of your suggestions are considered anti patterns and I would personally avoid both.
What I would personally do is very similar to your first suggestion but instead of checking the value of the connection string I would just use the process.env.NODE_ENV
value, this is considered common practice to use this to separate between your dev staging and prod enviorments.
Is there a way of mimicking the Atlas search behavior for a local MongoDB? It's okay if it involves a really slow query but it would be nice to test with same quality text matching as there will be in production.
Technically no, as you know atlas search gives you many search engine abilities such as tokenizers and analyzers out of the box. If you have a very specific use case maybe you could engineer a solution to make it work, but that will require you to basically reverse engineer the functionality you're using through atlas which means it's probably not worth the effort.
Edit:
Recently MongoDB added the options for a local atlas deployment, you can follow these docs.