Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!