mongodbmongo-shelldbref

MongoDB - DB Reference returns null value


output

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


Solution

  • From the Data Types documentation:

    Compared to the legacy mongo shell, MongoDB Shell(mongosh) has type handling which is better aligned with the default types used by the MongoDB drivers.

    For example, the constructor's signature of 5.6/classes/BSON.DBRef.html#constructor class in node-mongodb-driver:

    new DBRef(collection: string, oid: ObjectId, db?: string, fields?: Document): DBRef
    

    Properties:

    name type description
    collection string the collection name.
    oid ObjectId the reference ObjectId.
    db string optional db name, if omitted the reference is local to the current db.

    The user.address is an instance of DBRef class:

    Atlas atlas-zy5qpa-shard-0 [primary] test> user.address instanceof DBRef
    true
    

    So the command in mongosh should be:

    Atlas atlas-zy5qpa-shard-0 [primary] test> db[user.address.collection].findOne({_id: user.address.oid})
    {
      _id: ObjectId("534009e4d852427820000002"),
      building: '22 A, Indiana Apt',
      pincode: 123456,
      city: 'Los Angeles',
      state: 'California'
    }
    

    Or, use toJSON() method to get the BSON.DBRefLike data.

    Atlas atlas-zy5qpa-shard-0 [primary] test> dbRef.toJSON()
    {
      '$ref': 'address_home',
      '$id': ObjectId("534009e4d852427820000002"),
      '$db': 'test'
    }
    Atlas atlas-zy5qpa-shard-0 [primary] test> db[dbRef.toJSON().$ref].findOne({_id: dbRef.toJSON().$id})
    {
      _id: ObjectId("534009e4d852427820000002"),
      building: '22 A, Indiana Apt',
      pincode: 123456,
      city: 'Los Angeles',
      state: 'California'
    }
    

    The user document(Note: I use test database, not tutorialspoint):

    Atlas atlas-zy5qpa-shard-0 [primary] test> db.users.findOne({name: 'Tom Benzamin'})
    {
      _id: ObjectId("53402597d852426020000002"),
      address: DBRef("address_home", ObjectId("534009e4d852427820000002"), "test"),
      contact: '987654321',
      dob: '01-01-1991',
      name: 'Tom Benzamin'
    }
    

    mongosh version:

    $ mongosh --version
    1.10.1
    

    MongoDB version:

    Atlas atlas-zy5qpa-shard-0 [primary] test> db.version()
    6.0.6
    

    P.S. There is not even MongoDB and mongosh version information in tutorialspoint site's documentation. I guess I won't use it.