如何在多维数组中搜索值,例如我想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"
}
]
}