0
{
"_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”。请帮助!

4

2 回答 2

3

只需使用正则表达式

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

但是,是的,除非您使用新的文本搜索功能,否则此查询不会被编入索引

于 2013-01-24T08:04:49.617 回答
0

听起来您需要 mongoDB 的新文本搜索功能。另请查看 2.4发行说明

于 2013-01-24T07:45:58.657 回答