WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MongoDB - Database References


Referenced Relationships additionally called Manual References wherein we manually save the referenced record's id inner other record. However, in instances where a record incorporates references from exceptional collections, we can use MongoDB DBRefs.

DBRefs vs Manual References

As an instance scenario, in which we might use DBRefs in preference to guide references, recall a database wherein we are storing different types of addresses (home, office, mailing, and so on.) in different collections (address_home, address_office, address_mailing, and so forth).

Using DBRefs

There are 3 fields in DBRefs
  • $ref − This subject specifies the gathering of the referenced record

  • $identification − This discipline specifies the _id field of the referenced file

  • $db − This is an optional discipline and includes the call of the database wherein the referenced record lies

Consider a pattern consumer record having DBRef discipline address as proven inside the code snippet

    "_id":ObjectId("CfLbW+g4r9M.6F"),
    "cope with": 
    "$ref": "address_home",
    "$identification": ObjectId("CfLbW+g4r9M.6F"),
    "$db": "welookups",
    "touch": "987654321",
    "dob": "11-12-2000",
    "call": "David"

The cope with DBRef area right here specifies that the referenced deal with record lies in address_home series under welookups database and has an identification of CfLbW+g4r9M.6F. The following code dynamically looks inside the series distinctive with the aid of $ref parameter (address_home in our case) for a file with identification as precise through $id parameter in DBRef.
>var consumer = db.Customers.FindOne("name":"David")
>var dbRef = consumer.Deal with
>db[dbRef.$ref].FindOne("_id":(dbRef.$id))






Welookups is optimized for learning.Copy right 2018 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.