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

78
Vistas
MongoDB filter after populate javascript

I'm newer of nodejs, and I want to create API for my porfolio.

I have built API to get all of projects data, and I want to filter which use query after populate.

Project schema

const ProjectSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  description: {
    type: String,
  },
  tags: [
    {
      type: Schema.Types.ObjectId,
      ref: "tag",
    },
  ],
  createAt: {
    type: Date,
    default: Date.now,
  },
});

Tag schema

const TagSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  projects: [
    {
      type: Schema.Types.ObjectId,
      ref: "project",
    },
  ],
  createAt: {
    type: Date,
    default: Date.now,
  },
});

if ?tags=react, rwd I wish return:

[ 
   {
     name: "A Project",
     description: "blablabla",
     tag: [
       { name: "react" },
       { name: "rwd" },
       { name: "antd" ),
     ]
     createdAt: "2022/2/15"
   }, 
   {
     name: "B Project",
     description: "blablabla",
     tag: [
       { name: "react" },
       { name: "rwd" },
       { name: "mui" ),
     ]
     createdAt: "2022/2/15"
   },     
]

this is my code below:

// ?tags=react, rwd
const tags = req.query.tags.split(",");
const data = await Project.find()
      .sort({ number: -1 })
      .populate({
        path: "tags",
        select: "name -_id",
        match: { name: { $in: tags } },
      });

Can somebody help me to figure it out? thanks!

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