Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

like query in mongoDB

Tags:

mongodb

I am working on mongodb . In which i Want to use like query. my collection structure is as follows.

{ "name" : "xy" , "age" : 34 , "location" : "sss"}

{ "name" : "xyx" , "age" : 45 , "location" : "sshs"}

{ "name" : "x" , "age" : 33 , "location" : "shhss"}

{ "name" : "pq" , "age" : 23 , "location" : "hhh"}

{ "name" : "pqr" , "age" : 12 , "location" : "sss"}

i want to find records matching to "name" : "x".

so query will return all three records matching xy ,xyz,x.

Is it possible in mongo.

if any one knows plz reply.

Thanks

like image 915
Swapnil Sonawane Avatar asked Mar 17 '11 13:03

Swapnil Sonawane


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.

Can I use regex in MongoDB query?

MongoDB uses Perl compatible regular expressions(PCRE) version 8.42 along with UTF-8 support. In MongoDB, we can do pattern matching in two different ways: With $regex Operator. Without $regex Operator.

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 do I search for a part of a string in MongoDB?

Use the $text query operator to perform text searches on a collection with a text index. $text will tokenize the search string using whitespace and most punctuation as delimiters, and perform a logical OR of all such tokens in the search string.


1 Answers

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.

like image 143
Bryan Migliorisi Avatar answered Oct 13 '22 03:10

Bryan Migliorisi