如何将新记录插入多级MongoDB文档

时间:2019-01-24 11:40:08

标签: node.js 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',
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                },{
                   "login_id":"9937229854" ,
                   "loan_accounts_assigned": [{
                       lk_loan_account_id: '1234567'.
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                }]
            },{
                "location":"AIRPORT",
                "task_list":[{
                    "login_id":"9937229855",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '12345678',
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                }]
            }]
        },{
           "state":"JK",
            "location_list":[{
                "location":"NEW JK",
                "task_list":[{
                    "login_id":"9937229853",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456789',
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                },{
                   "login_id":"9937229857" ,
                   "loan_accounts_assigned": [{
                       lk_loan_account_id: '12345',
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                }]
            },{
                "location":"AIRPORT JK",
                "task_list":[{
                    "login_id":"9937229858",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456432',
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                }]
            }] 
        }]
    },{
        "zone":'EAST',
        "state_list":[{
            "state":"WB",
            "location_list":[{
                "location":"KOLKATA",
                "task_list":[{
                    "login_id":"9937229859",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456',
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                },{
                   "login_id":"9937229850" ,
                  "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456f',
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                }]
            },{
                "location":"ASAM",
                "task_list":[{
                    "login_id":"9937229895",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456456',
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                }]
            }]
        },{
           "state":"ODISHA",
            "location_list":[{
                "location":"BHUBANESWAR",
                "task_list":[{
                    "login_id":"9937229844",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456ty7',
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                },{
                   "login_id":"9937229845" ,
                   "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456',
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                }]
            },{
                "location":"AIRPORT BBSR",
                "task_list":[{
                    "login_id":"9937229846",
                    "loan_accounts_assigned": [{
                       lk_loan_account_id: '123456',
                       feedback_details:[
                         {name:'Raj'}
                       ] 
                    }]
                }]
            }] 
        }]
    }]
} 

在这里,我需要根据feedback_details将新记录插入login_id中,但是根据我的代码,我无法这样做。我在下面解释我的代码。

login_id=9937229853;
var data={
        name:"Rahul"
    }
    //console.log(data);
    db.collection.update(
    {},
    {$push:{"zone_list.$[].state_list.$[].location_list.$[].task_list.$[ts].loan_accounts_assigned.$[].feedback_details":data}}, 
    {arrayFilters:{"ts.login_id":login_id}}, function(uerr,udocs){
    //{"zone_list.state_list.location_list.task_list.login_id": login_id},
   // { $push : {"zone_list.state_list.location_list.task_list.loan_accounts_assigned.feedback_detail":data}},function(uerr,udocs){
        if (!uerr) {
            console.log('docs',udocs);
           var data1={
                "status": 200,
                "message": "Success"        
            } 
        }else{
            console.log('err',uerr);
            var data1={
                "status": 400,
                "message": "Failed"        
            } 
        }
        res.send(data1);
})

在这里我也遇到[Symbol(mongoErrorContextSymbol)]: {} }错误。我需要根据login_id将新记录插入feedback_detail中。

1 个答案:

答案 0 :(得分:0)

login_id=9937229853;
var data={
        name:"Rahul"
    }
    //console.log(data);
    db.collection.update(
    {"zone_list.state_list.location_list.task_list.login_id": login_id}, //add match filter also
    {$push:{"zone_list.$[].state_list.$[].location_list.$[].task_list.$[ts].loan_accounts_assigned.$[].feedback_details":data}}, 
    {arrayFilters:[{"ts.login_id":login_id}]} // u missed square brackets "[]" for array filter conditions
    ,function(uerr,udocs){
        if (!uerr) {
            console.log('docs',udocs);
           var data1={
                "status": 200,
                "message": "Success"        
            } 
        }else{
            console.log('err',uerr);
            var data1={
                "status": 400,
                "message": "Failed"        
            } 
        }
        res.send(data1);
})
  

@subhra:您错过了数组过滤器上的[]