Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

367
Views
Java Mongo - findAndReplace returns document dates in subkeys

I have a slight problem with return data from Mongo find() vs a findOneAndReplace().

First, to know I'm working on an API that queries Mongo and returns data in JSON format.

Problem I'm having is that if I do a findOneAndReplace() to update and return the modified document, much like so:

// javaDocument() is just a org.bson.Document
var modDoc = jobsCollection.findOneAndReplace( javaDocument({"_id": jobid}), javaDocument(jobData), foarOptions );

if(isDefined("modDoc")) {
  sReturn.DATA = deserializeJSON(modDoc.toJSON());
}

the dates set in my document returns within subkeys named "$date", which I do not want:

It really should read: "created_at": "2022-02-03T10:15:01.634Z"

Doing a simple jobsCollection.find() however, seems to return me the date appropriately, like so:

var data= [];
jobsCollection.find( javaDocument({}) ).into(data);
sReturn.DATA = data;

enter image description here

What am I missing here ? I could simple copy the "$date" key and fix the structure, but I don't always know where in the structure I'll have dates.... Is there a way I could have this properly returned with FindOneAndReplace() ? I'm thinking in might have something to do with the modDoc.toJSON() but haven't quite found my answer yet...

Thanks for your time. Cheers! Pat

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!