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

128
Vistas
Filtering objects by a search

I am trying to filter a list of products by a search parameter that will check for products that have the property name and seller.store.name similar (LIKE in php) to the search parameter.

viewAll: async (req, res) => {
    const search = req.query.search || '';
    const user = User.find({ store.name: { $regex: search, $options: 'i' } });

    const filter = search ? {
        name: { $regex: search, $options: 'i' },
        seller: { user }
    } : {};

    const products = await Product.find({ ...filter })
        .populate('store', 'store.name store.img');

    res.send({ products });
},

I am running into two problems.

  1. I cannot compare the store.name property using the db.collection.find method. I am getting the error ',' expected.ts(1005). I have tried using both store.name and store['name'].
  2. I would like the filter to be similar to the LIKE keyword in PHP and not an exact match. I'm not sure what function or options I should be using.

These are my models:

product.js

const mongoose = require('mongoose');

const productSchema = mongoose.Schema({
    name: { type: String, required: true },
    description: { type: String, required: true },
    seller: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    image: { type: String, required: true },
    price: { unitPrice: Number, unit: String, required: true },
    stock: {
        amtAvail: Number,
        amtSold: {
            type: Number,
            default: 0,
        },
        required: true
    },
    active: { type: Boolean, required: true, default: true }
}, {
    timestamps: true
});

const Product = mongoose.model('Product', productSchema);

module.exports = Product;

user.js

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
    name: { type: String, required: true, unique: true },
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    isSeller: { type: Boolean, default: false, required: true },
    store: {
        type: Object,
        name: { type: String, required: true, unique: true },
        description: { type: String, required: true },
        img: {
            profile: String,
            header: [String]
        },
        contact: {
            line: String,
            phone: Number,
            accountNumber: Number
        }
    },
    address: String,
}, {
    timestamps: true
});

const User = mongoose.model('User', userSchema);

module.exports = User;

Any help and ideas would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
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