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

233
Vistas
obtener datos de una matriz anidada en mongodb

Soy muy nuevo en mongodb. Acabo de comenzar mi proyecto usando mongodb. En mi proyecto, necesito obtener los datos de una matriz anidada. Sé que hay demasiadas preguntas relacionadas con esto, pero mi problema no está resuelto. aquí está json.

 { "_id": ObjectId("58805a04469d401ab45943f6"), "siteId": 2, "guardGender": "MALE", "checkoutPoint": [{ "_id": ObjectId("588098a025bad12cf01936d6"), "title": "string", "nfcCheckoutPoint": [{ "_id": ObjectId("5880c14683d042207896f7a4"), "title": "yup", "intervalTime": "string", }] }] }

Solo necesito la matriz nfcCheckoutPoint.

 "nfcCheckoutPoint": [{ "_id": ObjectId("5880c14683d042207896f7a4"), "title": "yup", "intervalTime": "string", }]

Este es mi código que estoy usando

 var criteria = { _id: payloadData.siteId, "checkoutPoint._id": payloadData.routeId, "checkoutPoint.nfcCheckoutPoint._id": payloadData.checkpointId }; var projection = { nfcCheckoutPoint: { $elemMatch: { _id: payloadData.checkpointId }}} var option = { lean: true }; Service.SiteService.getSite(criteria, projection, option, function (err, data) { if (err) { cb(err) } else { console.log(data)// this give me wrong values } }) var getSite = function (criteria, projection, options, callback) { options.lean = true; Models.Site.find(criteria, projection, options, callback); };

Con este código estoy obteniendo:

 { "_id": ObjectId("58805a04469d401ab45943f6")}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Puedes probar con una consulta aggregate

 db.getCollection('products').aggregate([ { "$match": { _id: payloadData.siteId} }, //siteId = ObjectId("58805a04469d401ab45943f6") { "$project": { "checkoutPoint": { "$filter": { "input": { "$map": { "input": "$checkoutPoint", "as": "cPoint", "in": { "_id": "$$cPoint._id", "nfcCheckoutPoint": { "$filter": { "input": "$$cPoint.nfcCheckoutPoint", "as": "nfcPoint", "cond": { "$eq": [ '$$nfcPoint._id', payloadData.checkpointId ] //checkpointId = ObjectId("5880c14683d042207896f7a4") } } } } } }, "as": "checkoutPoint", "cond": { "$eq": [ "$$checkoutPoint._id", payloadData.routeId]}// routeId =ObjectId("588098a025bad12cf01936d6") } } }}, {$project:{nfcCheckoutPoint:{$arrayElemAt: [ "$checkoutPoint.nfcCheckoutPoint", 0 ]}, _id:0}} ])

entonces la salida será como:

 { "nfcCheckoutPoint" : [ { "_id" : ObjectId("5880c14683d042207896f7a4"), "title" : "yup", "intervalTime" : "string" } ] }
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