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

261
Vistas
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 Respuestas
Responde la pregunta

0

Top of the your code add this:

const { ObjectId } = require('mongodb');
about 4 years ago · Juan Pablo Isaza Denunciar
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