Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

369
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda