User Tools

Site Tools


Sidebar






Old

staff:courses:no_sql_introduction:mongo_query_across_levels

This is an old revision of the document!


Задача: в БД с данными о видах и конкретных особях животных найти тех, кто питается одинаковым кормом. Исходные данные:

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 ] } ] }

staff/courses/no_sql_introduction/mongo_query_across_levels.1480334962.txt.gz · Last modified: 2022/12/10 09:08 (external edit)