How to insert an object containing an ObjectId with Realm

Hello, I’m working on a mongodb Realm app on Atlas. (Web SDK).

I’m trying to save an object containing on ObjectId with collection.insertOne(myObject)

But insertOne always returns “InvalidParameter Error”
If I save the myObject without the ObjectId, it works properly.

What I want to achieve is to copy entry._id (which is an ObjectId) into entryCopy.
How can I make this work ?


import { ObjectId } from "bson";

entry = {
  _id: (ObjectId generated by mongodb Atlas)
  field1: value1,
  field2: value2,
}

entryCopy = {
  _id: (ObjectId will be generated by mongodb Atlas)
  field1: value1,
  field2: value2,
  originalEntryId: new ObjectId(entry._id.id),
}

I’m not sure this will work but how about :

new ObjectId(entry._id.str)

Nope, doesn’t work either :disappointed:

I just moved away from bson-object to realm-web and heres what worked for me.

import {BSON} from "realm-web";
let id = BSON.ObjectID(BSON.ObjectID.generate());
let stringify = id.toHexString();