DBRef returns null value

As seen in this image when I try to retrieve address details from collection ‘address_home’ using DB Reference in MongoDB, it returns null value. Anybody know what is the problem with this code?

I tried the code in the image. I want it to print data from the ‘address_home’ collection in place of the ‘DBRef()’ data on users table.

use tutorialspoint

db.address_home.insertOne({ "_id": ObjectId("534009e4d852427820000002"), "building": "22 A, Indiana Apt", "pincode": 123456, "city": "Los Angeles", "state": "California" })

db.users.insertOne( { "_id": ObjectId("53402597d852426020000002"), "address": { "$ref": "address_home", "$id": ObjectId("534009e4d852427820000002"), "$db": "tutorialspoint" }, "contact": "987654321", "dob": "01-01-1991", "name": "Tom Benzamin" })

var user = db.users.findOne({"name":"Tom Benzamin"})
var dbRef = user.address

and finally,

db[dbRef.$ref].findOne({"_id":(dbRef.$id)})

but, this command returns null as seen in the image.

I tried this code from here:

Hello @Muhammed_Saajid ,

Welcome to The MongoDB Community Forums! :wave:

I noticed that you have not had a response to this topic yet, were you able to find a solution?
If not, then I would suggest you to use $lookup for this. As per documentation on Database References

This page outlines alternative procedures that predate the $lookup and $graphLookup pipeline stages.

$lookup - Performs a left outer join to a collection in the same database to filter in documents from the “joined” collection for processing. Below is an example for the same.

Perform a Single Equality Join with $lookup

Create a collection orders with these documents:

db.orders.insertMany( [   
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },   
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },   
{ "_id" : 3  }
] )

Create another collection inventory with these documents:

db.inventory.insertMany( [   
{ "_id" : 1, "sku" : "almonds", "description": "product 1", "instock" : 120 },   
{ "_id" : 2, "sku" : "bread", "description": "product 2", "instock" : 80 },   
{ "_id" : 3, "sku" : "cashews", "description": "product 3", "instock" : 60 },   
{ "_id" : 4, "sku" : "pecans", "description": "product 4", "instock" : 70 },   
{ "_id" : 5, "sku": null, "description": "Incomplete" },   
{ "_id" : 6 }
] )

The following aggregation operation on the orders collection joins the documents from orders with the documents from the inventory collection using the fields item from the orders collection and the sku field from the inventory collection:

db.orders.aggregate( [   
{    
 $lookup:      
 {        
 from: "inventory",        
 localField: "item",        
 foreignField: "sku",        
 as: "inventory_docs"      
 } 
 }
] )

The operation returns these documents:

{  
 "_id" : 1,  
 "item" : "almonds",  
 "price" : 12,  
 "quantity" : 2,  
 "inventory_docs" : [     
 { "_id" : 1, "sku" : "almonds", "description" : "product 1", "instock" : 120 }  
 ]
}
{  
 "_id" : 2,  
 "item" : "pecans",  
 "price" : 20,  
 "quantity" : 1,  
 "inventory_docs" : [     
 { "_id" : 4, "sku" : "pecans", "description" : "product 4", "instock" : 70 }  
 ]
}
{  
 "_id" : 3,  
 "inventory_docs" : [     
 { "_id" : 5, "sku" : null, "description" : "Incomplete" },     
 { "_id" : 6 }  
 ]
}

Regards,
Tarun

No, I got solution for DBRef. It was a mistake which was caused by an outdated syntax

1 Like