Can FindOne() return "typed JSON" (createdAt: {$date: "..."}, instead of createdAt: "..." etc.)

Normally, when using findOne() command, one will get an object that will look like this:

  id: "...",
  createdAt: "..."

I would like this object to be in the same form I can see when inserting documents in Atlas, or using mongoexport:

  id: {
    $oid: "..." 
  createdAt: {
    $date: "..."

Is that possible with the findOne() method, or some other approach?
I am trying to do it in a Realm function, so using database tools, like mongoexport, are not an option.

Hi @_alex,

Would that work for you?

exports = function(arg){
    const coll ="mongodb-atlas").db("test").collection("test");
    return coll.findOne().then((doc) => {
      const s = EJSON.stringify(doc);
      return s;


> ran on Tue Sep 28 2021 21:07:39 GMT+0200 (Central European Summer Time)
> took undefined
> logs: 
> result: 
> result (JavaScript): 

If I use the JSON helper instead of the EJSON one, I get this result instead:

> ran on Tue Sep 28 2021 21:08:42 GMT+0200 (Central European Summer Time)
> took undefined
> logs: 
> result: 
> result (JavaScript): 

Doc for the different helpers available:


1 Like

Thank you so much, @MaBeuLux88. This is exactly what I was looking for!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.