如何从MongoDB和Node.js中与文档匹配的用户输入中获取价值

时间:2019-01-23 10:25:39

标签: node.js mongodb

我在MongoDB中有一个多级文档,我需要通过匹配用户输入来获取一个单列值。我在下面解释我的文件。

{
    "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'
                    }]
                }]
            }] 
        }]
    }]
} 

在这里,如果需要login_id=9937229853,则需要使用MongoDB聚合函数获取其下的所有lk_loan_account_id值。

2 个答案:

答案 0 :(得分:1)

好吧,如果每个“ lk_loan_account_id”位于不同的对象中都可以,那么您可以尝试一下。

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

这会给类似的东西

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

否则,如果您只想要匹配的所有“ lk_loan_account_id”的单个数组,则可以使用它,

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

这将给出类似的信息

[
  {
    "_id": "9937229853",
    "lk_loan_account_id": [
      "123456",
      "123456789"
    ]
  }
]

答案 1 :(得分:0)

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

尝试一下。它将给出相同的结果。