MongoDB relationship-1-to-many embedded
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})