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

118
Views
Exactly match query in mongodb

I created an Express.js MongoDB API that filters products based on the filter property, the problem is that I want to make this API output exactly match the filter property. but currently for example if product A has [{name: 'a', value: '1'}, {name: 'b', value: '2'}] and product B has [{name: 'a', value: '1'}, {name: 'c', value: '3'}] and if I pass the data to the API like [{name: 'a', value: '1'}, {name: 'b', value: '2'}], it returns product B too, because it has {name: 'a', value: '1'} as a value inside filter property. How can I make my query exactly match based on passed values?

this is product schema:

const mongoose = require('mongoose');
const { s, rs, rn, rref, ref, b } = require('../utils/mongo');

let schema = new mongoose.Schema(
  {
    user: rref('user'),
    name: rs,
    description: s,
    images: [s],
    price: rn,
    categories: ref('category'),
    filters: [
      {
        parent: ref('filter'),
        value: s,
        name: s,
      },
    ],
    subFilter: [
      {
        parent: s,
        value: s,
        title: s,
      },
    ],
    published: b,
  },
  { timestamps: true }
);

module.exports = mongoose.model('product', schema);

and this is my query:

filter: async (req, res) => {
    try {
      const { categories, filters } = req.body;
      let products;

      if (filters.length > 0) {
        let targ_cat = categories;
        let any_one_of = filters;

        let or_list = [];

        any_one_of.forEach(function (f) {
          or_list.push({
            $and: [
              { $eq: [f['name'], '$$this.name'] },
              { $eq: [f['value'], '$$this.value'] },
            ],
          });
        });
        let or_expr = { $or: or_list };

        products = await Product.aggregate([
          { $match: { categories: new ObjectId(targ_cat) } },
          {
            $addFields: {
              filters: { $filter: { input: '$filters', cond: or_expr } },
            },
          },
          {
            $match: {
              $expr: { $gt: [{ $size: '$filters' }, 0] },
            },
          },
        ]);
      } else {
        products = await Product.find({ categories });
      }

      res.status(200).json(products);
    } catch (err) {
      console.log(err);
      res.status(500).json(err);
    }
  }

this one is what I want to send as body to the API

{
  category: '62445c3d922d127512867245'
  filters: [
    { name: 'filter name 1', value: '62445c3d922d127512861236' },
    { name: 'filter name 2', value: '62445c3d922d127512861458' },
    .....
  ]
}```
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use a simple find for exact match:

db.collection.find({
  category: "62445c3d922d127512867245",
  filters: [
    {
      name: "filter name 1",
      value: "62445c3d922d127512861236"
    },
    {
      name: "filter name 2",
      value: "62445c3d922d127512861458"
    }
  ]
})

See how it works on the playground example

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!