vscode icon indicating copy to clipboard operation
vscode copied to clipboard

How to create a new property those value is ObjectId?

Open upirr opened this issue 5 months ago • 1 comments

How to create a new property those value is ObjectId?

Question

Subject. I've tried doing: { $oid: }, of course it's not an ObjectId because of an obvious ambiguity. And when attempting to write ObjectId() - of course it complains about it not being a proper JSON

upirr avatar Jun 11 '25 13:06 upirr

How are you inserting the document? If you're inserting it via a playground, you should be able to do something like:

db.getCollection("foo").insertOne({
  _id: new ObjectId()
});

Alternatively, if you're editing an existing document, you should be able to use the ejson syntax and add a new field like:

{
  ...
  "my-new-field": {
    "$oid": "..."
  }
}

We're aware that this is not ideal experience and is something we're planning to overhaul in the near future, but don't have concrete timelines to share that. And of course, if neither of these scenarios cover what you're trying to do, please add some repro steps and I'll try and take a look.

nirinchev avatar Jun 11 '25 14:06 nirinchev