Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

231
Visualizações
get data from nested array in mongodb

I am very new in mongodb. I just start my project using mongodb.In my project i need to get the data from nested array. I know there are too many questions related to this, but my problem is not solved. here is json.

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

} 

I need only nfcCheckoutPoint array.

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

This is my code which i am using

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);
};

With this code i am getting:

 { "_id": ObjectId("58805a04469d401ab45943f6")}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can try with aggregate query

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}}
])

then output will be like:

{
    "nfcCheckoutPoint" : [ 
       {
          "_id" : ObjectId("5880c14683d042207896f7a4"),
          "title" : "yup",
          "intervalTime" : "string"
       }
     ]
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda