溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MongoDB 數(shù)據(jù)庫引用

發(fā)布時間:2020-07-01 22:29:22 來源:網(wǎng)絡(luò) 閱讀:376 作者:素顏豬 欄目:MongoDB數(shù)據(jù)庫

1.查看數(shù)據(jù)庫

show dbs

MongoDB 數(shù)據(jù)庫引用

2.創(chuàng)建runoob數(shù)據(jù)庫

use runoob

MongoDB 數(shù)據(jù)庫引用

3.查看runoob中集合

show collections

MongoDB 數(shù)據(jù)庫引用

4.創(chuàng)建地址集合(address_home,address_office)

db.createCollection("address_home")

MongoDB 數(shù)據(jù)庫引用

db.createCollection("address_office")

MongoDB 數(shù)據(jù)庫引用

5.查看創(chuàng)建的集合

show collections

MongoDB 數(shù)據(jù)庫引用

6.分別向家庭(address_home)地址集合和辦公(address_office)地址集合添加數(shù)據(jù)

db.address_home.insertOne({

'name':'home address',

'province':'jiangsu',

'city':'xuzhou'

})

MongoDB 數(shù)據(jù)庫引用

查看address_home數(shù)據(jù)

db.address_home.find().pretty()

MongoDB 數(shù)據(jù)庫引用

db.address_office.insertOne({

'name':'office address',

'province':'jiangsu',

'city':'nanjing'

})

MongoDB 數(shù)據(jù)庫引用

查看address_office數(shù)據(jù)

db.address_office.find().pretty()

MongoDB 數(shù)據(jù)庫引用

7.創(chuàng)建用戶集合(users)

db.createCollection("users")

MongoDB 數(shù)據(jù)庫引用

8.查看創(chuàng)建集合

show collections

MongoDB 數(shù)據(jù)庫引用

9.向users集合添加數(shù)據(jù)

db.users.insertOne({

"name":"suyanzhu",

"address":{

"$ref":"address_home",

"$id":ObjectId("5bd2b70fabb45c7371f36eda")

},

"age":18

})

MongoDB 數(shù)據(jù)庫引用

查看集合中的數(shù)據(jù)

db.users.find().pretty()

MongoDB 數(shù)據(jù)庫引用

10.查看用戶具體的地址信息

var user = db.users.findOne({"name":"suyanzhu"})

user

MongoDB 數(shù)據(jù)庫引用

var dbRef = user.address

dbRef

MongoDB 數(shù)據(jù)庫引用

var collName = dbRef.$ref

collName

MongoDB 數(shù)據(jù)庫引用

var id = dbRef.$id

id

MongoDB 數(shù)據(jù)庫引用

db[collName].findOne({"_id":id})

MongoDB 數(shù)據(jù)庫引用

MongoDB 數(shù)據(jù)庫引用

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI