Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
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 answers
Answer question

0

Top of the your code add this:

const { ObjectId } = require('mongodb');
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!