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

217
Vistas
How to match on the object level in MongoDB?

Input Documents

  "data": {
    "abc": {
      "Id": "100"

    },
    "xyz": {
      "Id": "123"
    }
}

Explanation : I want to do the $match on data.{i} i is parameter if I give to "abc" as a parameter I get the following output, I want to pass the multiple parameters to i "abc", "xyz".. How I can do that in do that $match the object key using parameter.

Expected Output :

  "data": {
    "abc": {
      "Id": "100"
    },
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

  • Check the key is exists or not using $exists
  • project that key,
let i = "abc";
Schema.find(
  { ["data."+i]: { $exists: true } },
  { ["data."+i]: 1 }
)

Playground


Second option if you have list of keys in array,

  • map through prepare a query for $or condition, and prepare project part
let i = ["abc", "xyz"];
let query = [], project = {};
i.map(k => {
  query.push({ ["data."+k]: { $exists: true } });
  project["data."+k] = 1;
});

Schema.find({ $or: query }, project);

Playground


Third option using project operators starting from MongoDb v4.4, with more dynamic approach,

  • $objectToArray convert object to array
  • $filter to filter above converted array and get matching elements
  • $arrayToObject convert array back to object
let i = "abc";
Schema.find(
  { ["data."+i]: { $exists: true } },
  {
    data: {
      $arrayToObject: {
        $filter: {
          input: { $objectToArray: "$data" },
          cond: { $eq: ["$$this.k", i] }
        }
      }
    }
  }
)

Playground

over 4 years ago · Santiago Trujillo Denunciar

0

Try this:

let keys = ["abc"];

db.collectionName.aggregate([
    {
        $project: {
            "filteredData": {
                $filter: {
                    input: { $objectToArray: "$data" },
                    as: "item",
                    cond: {
                        $in: ["$$item.k", keys]
                    }
                }
            }
        }
    },
    {
        $project: {
            data: { $arrayToObject: "$filteredData" }
        }
    }
]);

Output: When array keys = ["abc"];

{
    "_id" : ObjectId("60395e36e0c7d52970d3fa21"),
    "data" : {
        "abc" : {
            "Id" : "100"
        }
    }
}

Output: When array keys = ["abc", "xyz"];

{
    "_id" : ObjectId("60395e36e0c7d52970d3fa21"),
    "data" : {
        "abc" : {
            "Id" : "100"
        },
        "xyz" : {
            "Id" : "123"
        }
    }
}
over 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