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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With