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

From the Data Types documentation:
For example, the constructor's signature of 5.6/classes/BSON.DBRef.html#constructor class in node-mongodb-driver:
Properties:
The
user.addressis an instance ofDBRefclass:So the command in
mongoshshould be:Or, use toJSON() method to get the BSON.DBRefLike data.
The user document(Note: I use
testdatabase, nottutorialspoint):mongoshversion:MongoDB version:
P.S. There is not even MongoDB and
mongoshversion information in tutorialspoint site's documentation. I guess I won't use it.