Get the App
SLTechnology News&Howtos  ›  Database  › 

Application of MongoDB relation-1 to multi-reference relation

Shulou Source: shulou.com Published: 2022-06-01 12:59:29 10月01日 Update

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]

Tags: Data database creation information address user application Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Linux Xiaomi Shulou Technology