$addFields with $meta: "recordId" doesn't add new field

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Major - P3
    • 8.3.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Query Execution
    • Fully Compatible
    • ALL
    • QE 2025-10-13
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      RecordID (or RID) is MongoDB's internal unique identifier for documents. It should be able to be accessed with the {$meta: "recordId"} operator. This works for $project currently, but not for $addFields.

      We see that the new field, which should equal the recordId, is not added to the output:

      > db.newcoll.insert({a: 1})
      WriteResult({ "nInserted" : 1 })
      > db.newcoll.aggregate([{$addFields: {rid: {$meta: "recordId"}}}])
      { "_id" : ObjectId("65c281f44bb7df438ffb4d1e"), "a" : 1 }
      

      The correct output would also include a field "rid" with the corresponding recordID for the document.

      We can see this works correctly for $project:

      db.newcoll.aggregate([{$project:{rid: {$meta: "recordId"}} }])
      { "_id" : ObjectId("65c289a24bb7df438ffb4d2a"), "rid" : NumberLong(6) }
      

      See comments for more info.

            Assignee:
            Logan Karbiner
            Reporter:
            Vishnu Kaushik
            Votes:
            1 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: