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

260
Visualizações
Can't get to query MongoDB's ObjectId using Cypress

I'm trying to execute the following query

db.getCollection('name').find({_id : ObjectId("hereGoesAnId")})

I'm using mongodb module and use cy.task to query the db each time. This is working properly. In fact if I change id for another attribute to query, it will work like a charm. Problem is I can only identify the task by this id and the ObjectId is the one that's giving me issues.

If I try sending the query this way:

const result = await.collection.find({_id: `ObjectId(${varName})`}).toArray()

the result comes empty because it interprets ObjectId as a string and does not find anything.

However if I do it this way:

const result = await.collection.find({_id: ObjectId(`${varName}`)}).toArray()

I get an error saying "ObjectId is not defined...

I'm new to mongo and I'm not sure if there's a workaround for this?

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

0

Top of the your code add this:

const { ObjectId } = require('mongodb');
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