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

397
Visualizações
What benefit of using mongoose.Types.ObjectId instead of req.params.id to compare with the _id in Mongoose

For example

await Smartphones.findOne({_id: req.params.id})
await Smartphones.findOne({_id: ObjectId(req.params.id)})

To me it seem both return object, but I don't if it have any benefit of using ObjectId when comparing

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If you are using ObjectId(req.params.id) it will try to parse the "req.params.id" value to mongoDB Object Id, if its unable to parse it throws an error as "req.params.id" is not a valid mongo object id

over 4 years ago · Santiago Trujillo Relatório

0

If you are using MongoDB Compass to fetch some data, you can't use the _id as a simple string, there you should use ObjectId for getting the data. An _id in the document can be of any form except an array, and the default format is ObjectId. The ObjectId is not just a simple format in MongoDB, other than that of a format it has some use-cases too, you can refer to this link

over 4 years ago · Santiago Trujillo 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