Using Dbrefs
There are three fields in Dbrefs-
$ref
-This field specifies the collection of referenced documents
$id
-This field specifies the field that references the document _id
$db
-This is an optional field and contains the name of the database where the referenced document resides
Suppose a sample user document with a DBREF field address
, as shown in the code snippet-
{ "_id":ObjectId("348362491fjaskdlf2314"), "address": { "$ref": "address_home", "$id": ObjectId("sfaafdf4137832149fssa"), "$db": "yiibai"}, "contact": "13800138000", "dob": "1991-12-12", "name": "Maxsu" }
Using DBEF
New Collection Role Roles table
Db.role.insert ([{role: "admin"},{role: "Root"},{role: "Guest"}])
New User Table
Db.user.insert ([
{roleid:[new dbref (' role ', ObjectId ("59b7910c943eabe738230d34")), new Dbref (' Role ', ObjectId (" 59b7910c943eabe738230d35 "))]},
{roleid:[new dbref (' role ', ObjectId ("59b7914c943eabe738230d36")]}
])
' Role ' is the name of the role table you just created, and Objectid is the record of which role table the current user record is associated with (the ID in role)
Inquire:
Check the user record for the specified ID:
Check the 1th associated role information in the Roleid field in the specified ID
Check the 2nd associated role information in the Roleid field in the specified ID
Http://www.yiibai.com/mongodb
MongoDB Association Query