Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoDB relationship-1-to-many embedded

Shulou Source: shulou.com Published: 2022-06-01 17:52:49 11月03日 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 collection (using the embedded method, you can embed the user address into the user's document)

Db.sites.insertOne ({

"name": "Tom Hanks"

"contact": "987654321"

"dob": "01-01-1991"

"address": [

{

"building": "22A Indiana Apt"

"pincode": 123456

"city": "Los Angeles"

"state": "California"

}

{

"building": "23A, Indiana Apt"

"pincode": 123457

"city": "Los Angelea"

"state": "Californiaf"

}

]

})

5. View the data in the collection

Db.sites.find () .pretty ()

6. Query the user's address

Db.sites.findOne ({"name": "Tom Hanks"}, {"address": 1})

Tags: Data database user address embedded document method query Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MariaDB Linux Docker MySQL