Query MongoDB collection by UUID

Hi @MutedJam ,

In that thread I’ve also suggested that the MongoDB driver should support extended JSON. At least, it’s in the docs that the Node JS should support it. And Extended JSON it’s a valid JSON.

I’ve tried some code and it’s not very hard to support this.

const { ObjectId } = require('mongodb');
const { EJSON } = require('bson'); // this is a dependency of the latest mongodb package

const doc = {
  someId: { $oid: '2022-12-16' }
  created_at: { $date: '2022-12-16' }
};
const edoc = EJSON.parse(JSON.stringify(doc));

const doc = {
  some_id: { $oid: '63a1e9d62bb1e717acc27cc8' },
  created_at: { $date: '2022-12-16' }
};
const edoc = EJSON.parse(JSON.stringify(doc));

console.log(edoc.some_id);
console.log(edoc.some_id instanceof ObjectId);

console.log(edoc.created_at);
console.log(edoc.created_at instanceof Date);

this will output:

new ObjectId("63a1e9d62bb1e717acc27cc8")
true
2022-12-16T00:00:00.000Z
true

And using the MongoClient to insert document it will work fine:

{
    "_id" : ObjectId("63a1ee2c335276614d97b448"),
    "some_id" : ObjectId("63a1e9d62bb1e717acc27cc8"),
    "created_at" : ISODate("2022-12-16T00:00:00.000Z")
}

I hope this helps.