7

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

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@pingw33n has a good point. One approach is to add another field in the collection that stores the downcase value of the name, index it, and query it with the input as downcased.
@pingw33n Thanks for pointing that out. @blu Thats the approach I use in cases like this and it seems to work well, unless you really need case sensitivity, where you have one value of "Xx" and one of "xX" and they both are normalized to "xx".
Hello fiends ,db.customers.find( { name : /^pqr/i } ); this query works,but suppose i am giving whole word like pqr then it return only one records for above collections.but i want to match both the records matching pqr and pq . is it possible in mongodb. or i am doing something wrong . plz tell me . thanks
If you are searching for a whole word, you'll never get a partial word match. Why would you? If you are looking for something to do that, you might want to look into something like Solr\Lucene which is a search engine and has capability to do many more interesting things in regards to search.
-1

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

You may use regexes in database query expressions!

1 Comment

looking for mongoose support

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.