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

Application of MongoDB relation-1 to multi-reference relation

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

1. View the database

Show dbs

two。 Using the database

Use test

3. View collections in the database

Show collections

4. Add data to the address collection, which will be created if address does not exist

Db.address.insertMany ([

{

"building": "22A Indiana Apt"

"pincode": 123456

"city": "Los Angeles"

"state": "California"

}

{

"building": "23A, Indiana Apt"

"pincode": 123457

"city": "Los Angeles2"

"state": "California2"

}

])

5. View the data in the address collection

Db.address.find () .pretty ()

6. Add data to the users collection, creating if the users does not exist

Db.users.insertOne ({

"name": "Tom Benzamin"

"contact": "987654321"

"address_ids": [

ObjectId ("5bd27e95abb45c7371f36ed3")

ObjectId ("5bd27e95abb45c7371f36ed4")

]

})

7. View the data in the users collection

Db.users.find () .pretty ()

8. View the user's address information

Var result = db.users.findOne ({"name": "Tom Benzamin"}, {"address_ids": 1})

Var addresses = db.address.find ({"_ id": {"$in": result ["address_ids"]}})

Addresses [0]

Addresses [1]

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