1.查看数据库
show dbs
2.使用数据库
use test
3.查看数据库中的集合
show collections
4.向address集合添加数据,如果address不存在则会创建
db.address.insertMany([
{
"building": "22 A, Indiana Apt",
"pincode": 123456,
"city": "Los Angeles",
"state": "California"
},
{
"building": "23 A, Indiana Apt",
"pincode": 123457,
"city": "Los Angeles2",
"state": "California2"
},
])
5.查看address集合中的数据
db.address.find().pretty()
6.向users集合添加数据,如果users不存在会创建
db.users.insertOne({
"name":"Tom Benzamin",
"contact": "987654321",
"address_ids":[
ObjectId("5bd27e95abb45c7371f36ed3"),
ObjectId("5bd27e95abb45c7371f36ed4")
]
})
7.查看users集合中的数据
db.users.find().pretty()
8.查看用户的地址信息
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]