简体   繁体   中英

How to get value from document matching user input in MongoDB and Node.js

I have one multilevel document inside MongoDB and I need to fetch one single column value by matching user input. I am explaining my document below.

{
    "zone_list":[{
        "zone":'NORTH',
        "state_list":[{
            "state":"DELHI",
            "location_list":[{
                "location":"NEW DELHI",
                "task_list":[{
                    "login_id":"9937229853",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456'
                    }]
                },{
                   "login_id":"9937229854" ,
                   "loan_accounts_assigned": [{
                       lk_loan_account_id: '1234567'
                    }]
                }]
            },{
                "location":"AIRPORT",
                "task_list":[{
                    "login_id":"9937229855",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '12345678'
                    }]
                }]
            }]
        },{
           "state":"JK",
            "location_list":[{
                "location":"NEW JK",
                "task_list":[{
                    "login_id":"9937229853",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456789'
                    }]
                },{
                   "login_id":"9937229857" ,
                   "loan_accounts_assigned": [{
                       lk_loan_account_id: '12345'
                    }]
                }]
            },{
                "location":"AIRPORT JK",
                "task_list":[{
                    "login_id":"9937229858",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456432'
                    }]
                }]
            }] 
        }]
    },{
        "zone":'EAST',
        "state_list":[{
            "state":"WB",
            "location_list":[{
                "location":"KOLKATA",
                "task_list":[{
                    "login_id":"9937229859",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456'
                    }]
                },{
                   "login_id":"9937229850" ,
                  "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456f'
                    }]
                }]
            },{
                "location":"ASAM",
                "task_list":[{
                    "login_id":"9937229895",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456456'
                    }]
                }]
            }]
        },{
           "state":"ODISHA",
            "location_list":[{
                "location":"BHUBANESWAR",
                "task_list":[{
                    "login_id":"9937229844",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456ty7'
                    }]
                },{
                   "login_id":"9937229845" ,
                   "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456'
                    }]
                }]
            },{
                "location":"AIRPORT BBSR",
                "task_list":[{
                    "login_id":"9937229846",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456'
                    }]
                }]
            }] 
        }]
    }]
} 

Here I need if login_id=9937229853 then I need to fetch all lk_loan_account_id value coming under it using MongoDB aggregate function.

Well, if you're fine with each 'lk_loan_account_id' being in different objects, then you can try this.

    db.collection.aggregate([
  {
    $unwind: "$zone_list"
  },
  {
    $unwind: "$zone_list.state_list"
  },
  {
    $unwind: "$zone_list.state_list.location_list"
  },
  {
    $unwind: "$zone_list.state_list.location_list.task_list"
  },
  {
    $match: {
      "zone_list.state_list.location_list.task_list.login_id": "9937229853"
    }
  },
  {
    $project: {
      loan_accounts_assigned: "$zone_list.state_list.location_list.task_list.loan_accounts_assigned"
    }
  }
])

This will give something like,

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "loan_accounts_assigned": [
      {
        "lk_loan_account_id": "123456"
      }
    ]
  },
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "loan_accounts_assigned": [
      {
        "lk_loan_account_id": "123456789"
      }
    ]
  }
]

Else if you want just a single array of all the 'lk_loan_account_id' that matches, then you can use this,

    db.collection.aggregate([
  {
    $unwind: "$zone_list"
  },
  {
    $unwind: "$zone_list.state_list"
  },
  {
    $unwind: "$zone_list.state_list.location_list"
  },
  {
    $unwind: "$zone_list.state_list.location_list.task_list"
  },
  {
    $unwind: "$zone_list.state_list.location_list.task_list.loan_accounts_assigned"
  },
  {
    $match: {
      "zone_list.state_list.location_list.task_list.login_id": "9937229853"
    }
  },
  {
    $group: {
      _id: "$zone_list.state_list.location_list.task_list.login_id",
      lk_loan_account_id: {
        $push: "$zone_list.state_list.location_list.task_list.loan_accounts_assigned.lk_loan_account_id"
      }
    }
  }
])

This will give something like,

[
  {
    "_id": "9937229853",
    "lk_loan_account_id": [
      "123456",
      "123456789"
    ]
  }
]
db.mycollection.find({
    "zone_list.state_list.location_list.task_list.login_id": "someValue"
}).toArray((err,res)=>{
 if(err){
  console.log(err)
 }else {
  var reqData=[]
  res.forEach((data)=>{reqData.push(data.zone_list.state_list.location_list.task_list.lk_loan_account_id )})
  console.log(reqData) // you required
 }
})

Try This. It will give same results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM