如何使用mongodb在内部数组中搜索字符串? [英] How to search a string in inner array using mongodb?

查看:304
本文介绍了如何使用mongodb在内部数组中搜索字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to search value in multidimensional array, for example I want to search example keyword in the following data in mongodb I used to fetch all data from command

>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的数据,我使用了命令

Now, to find data having example I used command

>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个

But I want only 3 out of 5 sub rows like

{
    "_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"
            }
    ]
}

推荐答案

我尝试了Map Reduce Function,它可以处理此类问题,代码如下:

I tried Map Reduce Function and it works on this type of problems the code is something like that:

编写地图功能

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

编写归约函数

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

MapReduce函数

MapReduce Function

  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)

这篇关于如何使用mongodb在内部数组中搜索字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆