This shows you the differences between two versions of the page.
staff:courses:no_sql_introduction:mongo_query_across_levels [2016/11/28 12:09] mark created |
staff:courses:no_sql_introduction:mongo_query_across_levels [2022/12/10 09:08] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | Задача: в БД с данными о видах и конкретных особях животных найти тех, кто питается одинаковым кормом. Исходные данные: | ||
- | //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 ] } ] }// |