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

108
Vistas
MongoDB-mongoose, Array filter

I have an issue with array object.

    "userId": "63317e7f9746ba9719845b6e",
    "items": [
        {
            "productId": "Z357",
            "piece": 1,
            "_id": "63333427d7ea226b2462e734"
        },
        {
            "productId": "A541",
            "piece": 1,
            "_id": "6333342cd7ea226b2462e738"
        },
        {
            "productId": "G31",
            "piece": 1,
            "_id": "633334ea1ca2b582ddd0bc62"
        },
        {
            "productId": "K1123",
            "piece": 1,
            "_id": "6333351028b43b58b8f83ba3"
        }
    ],

I have an array like that. I want to get specific item with productId,

const checker = await Cart.findOne({
      "items.productId": productId,
    });

I'm checking an array like that, but it's giving me all of the items that contain productId, how can I get only what am I filter?

almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can try this way with projection to get only the matched element where your productId is equal to the provided productId,

const checker = await Cart.findOne({items:{ $elemMatch:{ productId: productId}}},
  {userId:1,"items.$": 1});
almost 4 years ago · Santiago Trujillo Denunciar
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