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.
- is related to
-
SERVER-86296 Existing $recordId field hidden when showRecordId:true is used
-
- Closed
-
-
SERVER-112276 Make record_id_meta.js only run in FCV 8.3 and above
-
- Closed
-