How to Query Mongodb With "Like"

How to query MongoDB with like

That would have to be:

db.users.find({"name": /.*m.*/})

Or, similar:

db.users.find({"name": /m/})

You're looking for something that contains "m" somewhere (SQL's '%' operator is equivalent to regular expressions' '.*'), not something that has "m" anchored to the beginning of the string.

Note: MongoDB uses regular expressions which are more powerful than "LIKE" in SQL. With regular expressions you can create any pattern that you imagine.

For more information on regular expressions, refer to Regular expressions (MDN).

MongoDB Query With Like

It looks like a typo. can you try it like this?

db.getCollection(“configurations”).find({"key" : /.*LOYALTY_SERVICE_ENABLED.*/}).pretty();

How to query MongoDB with like

That would have to be:

db.users.find({"name": /.*m.*/})

Or, similar:

db.users.find({"name": /m/})

You're looking for something that contains "m" somewhere (SQL's '%' operator is equivalent to regular expressions' '.*'), not something that has "m" anchored to the beginning of the string.

Note: MongoDB uses regular expressions which are more powerful than "LIKE" in SQL. With regular expressions you can create any pattern that you imagine.

For more information on regular expressions, refer to Regular expressions (MDN).

Mongo DB find - SQL Like - wildcard in the middle

Turns out this was a matter of escaping special characters ( and ), The query that worked is:

db.getCollection('collection_name').find({"some_field":{$regex:/^key->some_name::details->\(value = '.*'\):sort->by_name/}})

How to make LIKE query work in MongoDB?

Use a regular expression:

db.streets.find( { street_name : /^Al/i } );

or:

db.streets.find( { street_name : { $regex : '^Al', $options: 'i' } } );

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions

Turning this into PHP:

$regex = new MongoRegex("/^Al/i");
$collection->find(array('street_name' => $regex));

like query in mongoDB

You can use regular expressions to do this:

db.customers.find( { name : /^x/i } );

You will probably want to have some indexes on the name field.

Read more at the MongoDB Documetation site.

MONGO DB Like Operator

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.



Related Topics



Leave a reply



Submit