MongoDB database reference
1. View the database
Show dbs
two。 Create a runoob database
Use runoob
3. View collections in runoob
Show collections
4. Create address set (address_home,address_office)
Db.createCollection ("address_home")
Db.createCollection ("address_office")
5. View created collections
Show collections
6. Add data to the home (address_home) address set and the address_office address set, respectively
Db.address_home.insertOne ({
'name':'home address'
'province':'jiangsu'
'city':'xuzhou'
})
View address_home data
Db.address_home.find () .pretty ()
Db.address_office.insertOne ({
'name':'office address'
'province':'jiangsu'
'city':'nanjing'
})
View address_office data
Db.address_office.find () .pretty ()
7. Create a user collection (users)
Db.createCollection ("users")
8. View create collection
Show collections
9. Add data to the users collection
Db.users.insertOne ({
"name": "suyanzhu"
"address": {
"$ref": "address_home"
"$id": ObjectId ("5bd2b70fabb45c7371f36eda")
}
"age": 18
})
View the data in the collection
Db.users.find () .pretty ()
10. View the specific address information of the user
Var user = db.users.findOne ({"name": "suyanzhu"})
User
Var dbRef = user.address
DbRef
Var collName = dbRef.$ref
CollName
Var id = dbRef.$id
Id
DB [collName] .findOne ({"_ id": id})