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

235
Vistas
Mongoose: Get objects by a search query

I am trying to pull out data of 2 types from my BSON document using mongoose. The 2 data are;

  1. Intention 1 - I want to get objects from the items array where tagNo == 2
  2. Intention 2 -I want to get objects from the items array where tagNo == 2 and tagID == kLawOURVpz1IIjoQ2fhCvy7NM.

For intention 1, I tried;

 db.find(
  { "fileID": "0pdn3jdndndj3msms, "items.tagNo": 2 },
  {
    "items": {
      "$elemMatch": { "tagNo": 2 }
    }
  }
 );

and I tired the query below for intention 2

db.find(
 { "fileID": "0pdn3jdndndj3msms, "items.tagNo": 2 },
 {
   "items": {
      "$elemMatch": { "tagNo": 2, "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM", }
    }
  }
);

I keep getting the entire object back.

My BSON document is;

{
  "_id": "ID_GOES_HERE",
  "fileID": "0pdn3jdndndj3msms",
  "fileName": "Item List",
  "items": [
    {
      "tagNo": 2,
      "status": 0,
      "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
      "tagUnit": 5
    },
    {
      "tagNo": 2,
      "status": 0,
      "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
      "tagUnit": 8
    },
    {
      "tagNo": 2,
      "status": 0,
      "tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
      "tagUnit": 81
    },
    {
      "tagNo": 4,
      "status": 0,
      "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
      "tagUnit": 904
    },
    {
      "tagNo": 3,
      "status": 0,
      "tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
      "tagUnit": 24
    },
    {
      "tagNo": 2,
      "status": 0,
      "tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
      "tagUnit": 35
    },
    {
      "tagNo": 1,
      "status": 0,
      "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
      "tagUnit": 11
    },
    {
      "tagNo": 2,
      "status": 0,
      "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
      "tagUnit": 30
    }
  ]
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need aggregation framework

  db.collection.aggregate([
  {
    $match: { //Match condition to get the matching arrays
      "fileID": "0pdn3jdndndj3msms",
      "items.tagNo": 2
    }
  },
  {
    $project: {
      "items": {
        $filter: { //Project only matching array elements
          input: "$items",
          as: "item",
          cond: {
            $eq: [
              "$$item.tagNo",
              2
            ]
          }
        }
      }
    }
  }
])

Playground

db.collection.aggregate([
  {
    $match: {
      "items": {
        "$elemMatch": {
          "tagNo": 2,
          "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
          
        }
      }
    }
  },
  {
    $project: {
      "items": {
        $filter: {
          input: "$items",
          as: "item",
          cond: {
            "$and": [
              {
                $eq: [
                  "$$item.tagNo",
                  2
                ]
              },
              {
                $eq: [
                  "$$item.tagID",
                  "kLawOURVpz1IIjoQ2fhCvy7NM"
                ]
              }
            ]
          }
        }
      }
    }
  }
])

Playground

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