Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Data query of MongoDB (nested set)

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Each collection data in the MongoDB database can continue to save other collection data, for example, some people save family information.

Example: add data

Db.emp.insert ({"name": "Lin A", "sex": "male", "age": 22, "sal": 8000, "loc": "Beijing", "course": ["Chinese", "mathematics", "English", "music", "politics"], "parents": [{"name": "Lin A father", "age": 50, "job": "farmer"}, {"name": "Lin A mother", "age": 49 "job": "worker"}]})

Db.emp.insert ({"name": "Lin B", "sex": "male", "age": 30, "sal": 8000, "loc": "Beijing", "course": ["Chinese", "mathematics"], "parents": [{"name": "Lin B father", "age": 55, "job": "worker"}, {"name": "Lin B mother", "age": 52, "job": "farmer"}]})

Db.emp.insert ({"name": "Lin C", "sex": "male", "age": 35, "sal": 8000, "loc": "Beijing", "course": ["Chinese", "Mathematics", "English"], "parents": [{"name": "Lin C father", "age": 60, "job": "worker"}, {"name": "Lin C mother", "age": 59, "job": "staff"}]})

Db.emp.insert ({"name": "Lin D", "sex": "male", "age": 28, "sal": 8000, "loc": "Beijing", "course": ["Chinese", "Mathematics", "Politics"], "parents": [{"name": "Lin D Father", "age": 58, "job": "Director"}, {"name": "Lin D Mother", "age": 54, "job": "Director"]})

Db.emp.insert ({"name": "Lin E", "sex": "male", "age": 40, "sal": 8000, "loc": "Beijing", "course": ["language", "politics"], "parents": [{"name": "Lin E father", "age": 70, "job": "worker"}, {"name": "Lin E mother", "age": 68, "job": "Secretary"}]})

The content at this time is a nested collection, and the judgment of the data of this collection can only be done through "$elemMatch".

Example: find out the information of the director among the parents.

> db.emp.find ({"$and": [{"age": {"$gte": 30}}, {"parents": {"$elemMatch": {"job": "Director"}}]}) .pretty ()

{

"_ id": ObjectId ("599148bf0184ff511bf02b95")

"name": "Lin E"

"sex": "male"

"age": 40

"sal": 8000

"loc": "Beijing"

"course": [

"Chinese"

"politics"

]

"parents": [

{

"name": "Father Lin E"

"age": 70

"job": "worker

}

{

"name": "Mother Lin E"

"age": 68

"job": "Director"

}

]

}

Since the conditions of this kind of query are troublesome, try not to be so complicated if possible.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report