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

162
Visualizações
Make mongoose lean() populated queries return POJOs without any reused objects

If I use Mongoose to run a query on a schema that has a reference to another model (in this case, a BlogPost model with a property author which references another Author model).

let posts = await BlogPost.find({}, "title author").populate("author","name bio").lean()

By passing lean() I am supposed to get a POJO back like this:

[{
    "_id": "602828eaff09bca9c3af2dbb",
    "author": {
        "_id": "602433bd98d380443ef9aeb7",
        "name": "Matt",
        "bio": "matt is cool"
    },
    "title": "Hello World",
    "__v": 0
}, {
    "_id": "6048b8cb0200e51b4f2486c8",
    "author": {
        "_id": "602433bd98d380443ef9aeb7",
        "name": "Matt",
        "bio": "matt is cool"
    },
    "title": "Another Blog post",
    "__v": 0
}]

However if I modify for example

posts[0].author.name = "Test"

then ALL the posts with this author in the array are modified:

[{
    "_id": "602828eaff09bca9c3af2dbb",
    "author": {
        "_id": "602433bd98d380443ef9aeb7",
        "name": "Test",
        "bio": "matt is cool"
    },
    "title": "Hello World",
    "__v": 0
}, {
    "_id": "6048b8cb0200e51b4f2486c8",
    "author": {
        "_id": "602433bd98d380443ef9aeb7",
        "name": "Test",
        "bio": "matt is cool"
    },
    "title": "Another Blog post",
    "__v": 0
}]

If instead I only wanted to affect the first object in the array, what should I do?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The populate function in Mongoose https://mongoosejs.com/docs/api/query.html#query_Query-populate has an option clone:true

[options.clone=false] «boolean» When you do BlogPost.find().populate('author'), blog posts with the same author will share 1 copy of an author doc. Enable this option to make Mongoose clone populated docs before assigning them.

So the correct code would be something like

let posts = await BlogPost.find({}, "title author").populate({path:"author", fields:"name bio", clone:true}).lean()

Thanks to @buithienquyet in the comments for the pointers

about 4 years ago · Juan Pablo Isaza Relatório
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