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

116
Views
MongoDB query doesn't find the requested entries

i'm trying to show a user specific data using req.session.user and pass the ID to the criteria i'm building. (every entry also has a user field so i can match) yet it does not work.

The Service :

async function query(filterBy) {
  try {
    const criteria = _buildCriteria(filterBy);
    const collection = await dbService.getCollection('tab');
    const tabs = await collection.find(criteria).toArray();
    // const userTabs = await collection.find({ user: '62be030cb4de461a8462b863' }).toArray();
    return tabs;
  } catch (err) {
    logger.error('Can not find tabs', err);
    throw err;
  }
}

The console.log('userId', userId) returns the Id I get from my controller

function _buildCriteria(filterBy) {
  const criteria = {};
  const { text, genre, userId } = filterBy;
  console.log('userId', userId);

  if (text) {
    const txtCriteria = { $regex: text, $options: 'i' };
    criteria.name = txtCriteria;
  }
  if (genre) {
    criteria.genre = { $eq: genre };
  }
  if (userId) {
    criteria.user = { $eq: userId };
  }
  return criteria;
}

The controller :

async function getTabs(req, res) {
  try {
    const userId = req?.session?.user?._id;
    const filterBy = req.query;
    const fitlerUpdated = { ...filterBy, id: userId };
    const tabs = await tabService.query(fitlerUpdated);
    res.json(tabs);
  } catch (err) {
    logger.error('Failed to get tabs', err);
    res.status(500).send({ err: 'Failer ti get tabs' });
  }
}

I tried using

const userTabs = await collection.find({ user: '62be030cb4de461a8462b863' }).toArray()

and it works yet it doens't work along with the criteria.

thanks for any help!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I have realize I accidentally passed the wrong key. should have been id and not userId

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!