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"
}
]
}
Now, to find data having example I used command
>db.info.find({"info.email":"example"})
and it gives
{
"_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"
}
]
}
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"
}
]
}
I tried Map Reduce Function and it works on this type of problems the code is something like that:
Write a map function
Write a reduce function
MapReduce Function
And The Output look likes:
Now you can find your search data from