DBRef "$ref" containing dots gets split and stored as "$db"

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: 1.49.12
    • Component/s: None
    • None
    • Environment:
      OS: MacOS 26.5.2
      Compass Version: 1.49.12
    • None
    • None
    • Developer Tools

      Problem Statement/Rationale

      A collection named "foo.bar" is perfectly valid. However in compass DBRef would display it as 

      DBRef('bar', ObjectId('6a73061f8f257c0343126b85'), 'foo')

      Expected

      DBRef('foo.bar', ObjectId('6a73061f8f257c0343126b85'))

      Steps to Reproduce

       

      Env: Python 3
      python -m pip install pymongo
      from pymongo import MongoClient
      from bson import DBRef, ObjectId
      from bson.codec_options import CodecOptions
      from bson.raw_bson import RawBSONDocument
      import bson
      
      client = MongoClient("mongodb://localhost:27017/")
      db = client["testdb"]
      
      db.drop_collection("foo.bar")
      db.drop_collection("baz")
      
      # 1. Insert a document into a collection whose name itself contains a dot.
      inserted = db["foo.bar"].insert_one({"name": "hello"})
      target_id = inserted.inserted_id
      print("Inserted into 'foo.bar':", target_id)
      
      # 2. Build a DBRef pointing at that collection, using ONLY collection + id
      #    (no database arg) -- this is the correct way to reference it.
      ref = DBRef("foo.bar", target_id)
      print("Constructed DBRef:", ref)
      print("  ref.collection:", ref.collection)
      print("  ref.database:", ref.database)
      
      # 3. Store the DBRef in another document and read it back.
      db["baz"].insert_one({"ref": ref})
      readback = db["baz"].find_one()
      print("Read back via pymongo:", readback)
      print("  readback['ref'].collection:", readback["ref"].collection)
      print("  readback['ref'].database:", readback["ref"].database)
      
      # 4. Inspect the raw BSON bytes actually stored on disk, bypassing
      #    pymongo's automatic DBRef decoding, to prove nothing got split.
      raw_coll = db.get_collection("baz", codec_options=CodecOptions(document_class=RawBSONDocument))
      raw_doc = raw_coll.find_one()
      print("Raw BSON bytes:", raw_doc.raw)
      print("Decoded raw BSON:", bson.decode(raw_doc.raw))

      Output

      Inserted into 'foo.bar': 6a74f7612f58af1a5a376b87
      Constructed DBRef: DBRef('foo.bar', ObjectId('6a74f7612f58af1a5a376b87'))
        ref.collection: foo.bar
        ref.database: None
      Read back via pymongo: {'_id': ObjectId('6a74f7612f58af1a5a376b88'), 'ref': DBRef('foo.bar', ObjectId('6a74f7612f58af1a5a376b87'))}
        readback['ref'].collection: foo.bar
        readback['ref'].database: None
      Raw BSON bytes: b'C\x00\x00\x00\x07_id\x00jt\xf7a/X\xaf\x1aZ7k\x88\x03ref\x00(\x00\x00\x00\x02$ref\x00\x08\x00\x00\x00foo.bar\x00\x07$id\x00jt\xf7a/X\xaf\x1aZ7k\x87\x00\x00'
      Decoded raw BSON: {'_id': ObjectId('6a74f7612f58af1a5a376b88'), 'ref': DBRef('foo.bar', ObjectId('6a74f7612f58af1a5a376b87'))}
      

       

       

      Expected Results

      Actual Results

      Additional Notes

      This is only a display bug within Compass; at the DB level, it's stored as expected, as confirmed in the Python snippet.

      DBRef('foo.bar', ObjectId('6a74f7612f58af1a5a376b87'))

       

        1. image-2026-08-07-02-37-00-062.png
          89 kB
          Aswin Kishore Murali(SB,IN) (EXT)
        2. image-2026-08-07-02-37-38-827.png
          111 kB
          Aswin Kishore Murali(SB,IN) (EXT)
        3. image-2026-08-07-02-39-28-058.png
          120 kB
          Aswin Kishore Murali(SB,IN) (EXT)
        4. image-2026-08-07-02-42-20-621.png
          33 kB
          Aswin Kishore Murali(SB,IN) (EXT)

            Assignee:
            Unassigned
            Reporter:
            Aswin Kishore Murali(SB,IN) (EXT)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: