Home > Blockchain >  Mongoose find but ignore special characters in db value
Mongoose find but ignore special characters in db value

Time:10-16

I'm trying to do a mongoose find but I need to ignore special characters that exist in my database value.

I have a product called The Tyrant's Fist but I have an incoming value of The Tyrants Fist (product.name). How can I ignore ' and other special characters in my database version?

Product.find({ "name": product.name })

Thanks

CodePudding user response:

I would advice to add some new field like "normalized_name" where you store values the way you are going to search it, for example without some characters in it.

Other, but less performant way can be to use aggregation where you at first replace/remove those characters in name field and than do a $match stage using search term.

Collation may also be a good place to look at for certain scenarios.

  • Related