MongoDB aggregation return uInt8Array IDs

A simple $geoNear aggregation returns all my ObjectIds in the form of uInt8Arrays, and I cannot find out why:

let query = [
            {$geoNear: {
                near: geolocation,
                spherical: true,
                distanceField: "distance",
                maxDistance: 50000,
                distanceMultiplier : 0.000621371, //meters to miles
                query: {
                    "field": true
                }
            }},
            {$sort: {distance: -1}}
        ];

all IDs then look like this instead of the usual ObjectId("hexastring") format

_id: Object
  id: Uint8Array(12)
    0: 204
    1: 5
    2: 102
    3: 4
    4: 0
    5: 223
    6: 92
    7: 208
    8: 42
    9: 230
    10: 236
    11: 194
  _bsontype: "ObjectID"

I am able to convert them to ObjectIds using

newObject._Id = new Mongo.ObjectID(newObject._id.toString());

but this is suboptimal and does not allow for querying for IDs in the actual query, as I do not know how to convert a normal ObjectId to the uInt8Array format (which caused this thread).

Any ideas?

Thanks in advance!

1 Like

Any ideas here? I am currently avoiding this by searching for a name field but this obviously is a bad workaround.

Can you use $group or $addFields to rewrite _id as a string in the aggregation pipeline?

Sorry for the incredibly tardy reply. It would be great if you could let me know how I could I rewrite it using $group or $addFields?

I can use match.objectId = new Mongo.ObjectID(match.object._id.toString()); to reformat on the server but it does still feel like a hack.