Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

368
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda