Задача: в БД с данными о видах и конкретных особях животных найти тех, кто питается одинаковым кормом. Исходные данные:
db.animals.insert({animal_type:"l1", individuals: [ {name: "", text: "", feed: [1, 2] }, {name: "", text: "", feed: [3,4] }]})
db.animals.insert({animal_type:"l2", individuals: [ {name: "", text: "", feed: [1, 2] }, {name: "", text: "", feed: [2] }]})
db.animals.insert({animal_type:"l3", individuals: [ {name: "", text: "", feed: [1, 2] }, {name: "", text: "", feed: [3] }]})
Решение:
**db.animals.find({'individuals.feed': 3})**
Вывод:
{
"_id" : ObjectId("5834832075ec6ecba9a62c11"),
"animal_type" : "l1",
"individuals" : [
{ "name" : "", "text" : "", "feed" : [ 1, 2 ] },
{ "name" : "", "text" : "", "feed" : [ 3, 4 ] }
]
}
{
"_id" : ObjectId("5834833975ec6ecba9a62c13"),
"animal_type" : "l3",
"individuals" : [
{ "name" : "", "text" : "", "feed" : [ 1, 2 ] },
{ "name" : "", "text" : "", "feed" : [ 3 ] }
]
}