2

如何在多维数组中搜索值,例如我想example在mongodb的以下数据中搜索关键字我曾经从命令中获取所有数据

>db.info.find()

{
    "_id" : ObjectId("4f74737cc3a51043d26f4b90"),
    "id" : "12345",
    "info" : [
            {
                    "sno" : 1,
                    "name" : "ABC",
                    "email" : "abc@example.com"
            },
            {
                    "sno" : 2,
                    "name" : "XYZ",
                    "email" : "xyz@example.com"
            },
            {
                    "sno" : 3,
                    "name" : "XYZ",
                    "email" : "xyz@demo.com"
            },
            {
                    "sno" : 4,
                    "name" : "ABC",
                    "email" : "abc@demo.com"
            },
            {
                    "sno" : 5,
                    "name" : "Rohan",
                    "email" : "rohan@example.com"
            }
    ]
}

现在,要查找example我使用命令的数据

>db.info.find({"info.email":"example"}) 它给了

{
    "_id" : ObjectId("4f74737cc3a51043d26f4b90"),
    "id" : "12345",
    "info" : [
            {
                    "sno" : 1,
                    "name" : "ABC",
                    "email" : "abc@example.com"
            },
            {
                    "sno" : 2,
                    "name" : "XYZ",
                    "email" : "xyz@example.com"
            },
            {
                    "sno" : 3,
                    "name" : "XYZ",
                    "email" : "xyz@demo.com"
            },
            {
                    "sno" : 4,
                    "name" : "ABC",
                    "email" : "abc@demo.com"
            },
            {
                    "sno" : 5,
                    "name" : "Rohan",
                    "email" : "rohan@example.com"
            }
    ]
}

但我只想要 5 个子行中的 3 个,例如

{
    "_id" : ObjectId("4f74737cc3a51043d26f4b90"),
    "id" : "12345",
    "info" : [
            {
                    "sno" : 1,
                    "name" : "ABC",
                    "email" : "abc@example.com"
            },
            {
                    "sno" : 2,
                    "name" : "XYZ",
                    "email" : "xyz@example.com"
            },
            {
                    "sno" : 5,
                    "name" : "Rohan",
                    "email" : "rohan@example.com"
            }
    ]
}
4

4 回答 4

2

Rohan,MongoDB 总是返回您正在搜索的整个文档。您不能只让它返回找到关键字的数组元素。如果你想这样做,那么你需要确保“信息”字段中的所有嵌入文档都在它们自己的集合中。这可能意味着您需要将它们链接回“信息”集合中的原始文档。也许是这样的:

{
    "sno" : 1,
    "name" : "ABC",
    "email" : "abc@example.com"
    "info_id" : "12345",
},

或者,您当然可以在 PHP 中进行后处理以仅获取您想要的行。

于 2012-04-06T10:33:18.793 回答
1

我尝试了 Map Reduce Function,它适用于此类问题,代码如下所示:

编写地图函数

  map=function () 
  {
      filter = [];
      this.info.forEach(function (s) {if (/example/.test(s.email)) {filter.push(s);}});
      emit(this._id, {info:filter});
  }

写一个reduce函数

  reduce=function(key, values) { return values;}

MapReduce 函数

  res=db.info.mapReduce(map,reduce,{out:{inline:1}})

输出看起来像:

"results" : [
    {
            "_id" : ObjectId("4f9a2de0ea4a65c3ab85a9d3"),
            "value" : {
                    "info" : [
                            {
                                    "sno" : 1,
                                    "name" : "ABC",
                                    "email" : "abc@example.com"
                            },
                            {
                                    "sno" : 2,
                                    "name" : "XYZ",
                                    "email" : "xyz@example.com"
                            },
                            {
                                    "sno" : 5,
                                    "name" : "Rohan",
                                    "email" : "rohan@example.com"
                            }
                    ]
            }
    }
    ],
    "timeMillis" : 1,
    "counts" : {
            "input" : 3,
            "emit" : 3,
            "reduce" : 0,
            "output" : 3
    },
    "ok" : 1,

现在您可以从以下位置找到您的搜索数据

   printjson(res.results)
于 2012-04-27T05:49:19.983 回答
1

也许这是个好主意? http://php.net/manual/en/class.mongoregex.php

于 2012-04-06T10:23:32.797 回答
0

你试过$(投影)吗?

db.info.find({"info.email":"example"}, {"info.email.$":1})

文档

于 2013-11-07T07:17:03.297 回答