ArunKolhapur
ArunKolhapur

Reputation: 6915

How can i use both ways wildcards in mongodb?

{
"_id" : ObjectId("50ff7c427995fe330400000a"),
"group_name" : "College",
"owner" : "50f69176904e1d66affec20d",
"users" : [
    {
        "id" : "50f69176904e1d66affec20d"
    },
    {
        "id" : "50f69176904e1d66affec20d"
    },
    {
        "id" : "50f69176904e1d66affec20d"
    }
]
}
{
"_id" : ObjectId("50ff88e17995fe3204000002"),
"group_name" : "PHP",
"owner" : "50f69176904e1d66affec20d",
"users" : [
    {
        "id" : "50f69176904e1d66affec20d"
    }
],
"visibility" : "public"
}
{
"_id" : ObjectId("50ff86e17995fe3204000001"),
"group_name" : "Lithium",
"owner" : "50f69176904e1d66affec20d",
"users" : [
    {
        "id" : "50f69176904e1d66affec20d"
    }
],
"visibility" : "public"
}
{
"_id" : ObjectId("50ff98397995fe3204000003"),
"group_name" : "MongoDB",
"owner" : "50f69176904e1d66affec20d",
"users" : [
    {
        "id" : "50f69176904e1d66affec20d"
    }
],
"visibility" : "public"
}

In the above collection, i want to use wildcards to get group_name. The wildcard should be both ways, i.e; if i enter "h" in the search string then I should be able to get both "Lithium" and "PHP".. Help Please !!

Upvotes: 0

Views: 1890

Answers (2)

Jonathan Ong
Jonathan Ong

Reputation: 20315

Just use a regex

db.collection.find({
  'group_name': /.*h.*/i
}, callback)

but yeah, this query won't be indexed unless you use the new text search feature

Upvotes: 3

zemirco
zemirco

Reputation: 16395

Sounds like you need mongoDB's new text search features. Also take a look at the 2.4 release notes.

Upvotes: 0

Related Questions