Find documents with case insensitive, ignoring accents and % like

5,107 views
Skip to first unread message

Christian Cardozo

unread,
Apr 14, 2016, 6:52:09 PM4/14/16
to mongodb-user
Hi, i'm trying to make a query that ignore case sensitive, ignore diacritic sensitive(accents) and use the same logic that % (percent) in SQL.
I have in my database names like: "Palácio Tiradentes", "Palácio da Cidade", etc. I would like to make a search that ignore those things and use the percent like logic

For example, I would make like this in SQL:

select * from collection where remove_accents(upper(name)) like '%' + remove_accents(upper(query)) + '%'

where + concats string.



I found same semi-solutions:

1. I can use regexp to make the percent logic, like this:

db.col.find( { "name": { $regex: new RegExp(".*" + query + ".*", "i")} } )

This ignore case sensitives and make the percent logic for "containing" query string. But still with accents problem.

2. $text index:


Ok, this can ignore case sensitive and accents. But, in this case I can't use regular expressions in a query like this:

db.col.find( { $text: { $search:  ".*rest.*"} } } )

The regular expression isn't interpreted.

Someone has a ideia how can I implement this search query?

Thanks for helping :)

Christian Cardozo

unread,
Apr 17, 2016, 1:53:01 PM4/17/16
to mongodb-user

DAVID MAURICIO Moreno

unread,
Nov 22, 2018, 9:59:28 PM11/22/18
to mongodb-user

This work to me:

1. First of all I created a index like this: 

if (Meteor.isServer) {
MyCollection._ensureIndex({
"field": "text"
});
}


2.Then, to get data in array:


let busqueda = "some to search";
let data = MyCollection.find({
$text:
{
$search: '"'+busqueda+'"',
$caseSensitive: false,
$diacriticSensitive: false
}
}).fetch();

I hope this be helpfull


Reply all
Reply to author
Forward
0 new messages