Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MONGO DB Like Operator

I'm using JavaScript to query an API based on a Mongo DB.

I would need filter the result based on LIKE operator, something similar to

select * from playlist where title like '%a%'

At the moment I call this URL

var assetUrl = 'https://example.com/playlist?oauth_token=' + accessToken + '&account=XXX'+ '&fields={"title":true,"splash":true,"description":true,"source":true}'+ '&criteria={"title":/.*a.*/}';

With no success (Return 0 objects).

I would like to know if I should use Regular Expressions instead and how to use them in this context. Thanks

like image 610
GibboK Avatar asked Nov 30 '12 08:11

GibboK


People also ask

How do I write a like statement in MongoDB?

We can implement the functionality of the like query in Mongo DB by using the find() function in the format – db. collection. find(). We can specify the string, regex, or regular expression for matching the values in the parameters of the find() function.

How do I use wildcards in MongoDB query?

Create a Wildcard Index on All Fields With this wildcard index, MongoDB indexes all fields for each document in the collection. If a given field is a nested document or array, the wildcard index recurses into the document/array and stores the value for all fields in the document/array.

How regex works in MongoDB?

MongoDB provides the functionality to search a pattern in a string during a query by writing a regular expression. A regular expression is a generalized way to match patterns with sequences of characters. MongoDB uses Perl compatible regular expressions(PCRE) version 8.42 along with UTF-8 support.

What is comparison operator in MongoDB?

Comparison OperatorsMatches if values are greater than the given value. Matches if values are less than the given value. Matches if values are greater or equal to the given value. Matches if values are less or equal to the given value.


1 Answers

Yes, MongoDB supports regular expressions. You can read about it in the documentation. Here is an example:

db.collection.find( { url: /.*a.*/ } );

This finds all documents in the collection where the field "url" matches the regular expression. There is also an alternative syntax using the $regex operator:

db.collection.find( { url: { $regex: ".*a.*"} } );

Note that regular expressions are slow and scale badly. The search time is linear to the number of records in the collection, and indices only help when your regular expression begins with a begin-of-string anchor ^ (thanks, chx).

The documentation also has a chapter about Full Text Search in Mongo which recommends to split each string into an array of individual words, so that you can index it for faster lookup. This of course doesn't allow to search for word fragments, but greatly speeds up search for complete words.

Update: MongoDB 2.4 has a new experimental text-index feature which allows to speed up text-search with indices.

Update2: As of version 2.6, text search is enabled by default and ready for productive use.

like image 175
Philipp Avatar answered Oct 09 '22 21:10

Philipp