MongoDB Association Query

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.