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: https://www.tutorialspoint.com/mongodb/mongodb_database_references.htm