我如何在mongodb中使用两种通配符?

时间:2013-01-24 05:59:54

标签: 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"
}

在上面的集合中,我想使用通配符来获取group_name。通配符应该是两种方式,即;如果我在搜索字符串中输入“h”,那么我应该能够同时获得“Lithium”和“PHP”..请帮助!!

2 个答案:

答案 0 :(得分:3)

只需使用正则表达式

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

但是,除非您使用新的文本搜索功能

,否则不会对此查询编制索引

答案 1 :(得分:0)

听起来你需要mongoDB的新text search功能。另请查看2.4 release notes