1

This is the structure of the json object that is inside one of the fields called campaignResponse.

 campaignResponse:
   { prop:
      { summary: 't',
        retailer: '4',
        sent: 't',
        queued: 'f',
        open: 'f',
        spam: 't',
        click: 't',
        blocked: 'f',
        bounce: 'f',
        unsub: 'f',
        To: 'kartooot@gmail.com',
        status: 'clicked',
        subject: '',
        messageid: '20829150339986762',
        From: '',
        details: '',
        date: '2017-04-03T12:51:38' },
     camp2:
      { date: '2017-04-03T12:51:38',
        details: '',
        From: '',
        messageid: '20829150339986762',
        subject: '',
        status: 'clicked',
        To: 'kartooot@gmail.com',
        unsub: 'f',
        bounce: 'f',
        blocked: 'f',
        click: 't',
        spam: 'f',
        open: 't',
        queued: 'f',
        sent: 't',
        retailer: '4',
        summary: 'f' },
     camp1:
      { date: '2017-04-03T12:51:38',
        details: '',
        From: '',
        messageid: '20829150339986762',
        subject: '',
        status: 'clicked',
        To: 'kartooot@gmail.com',
        unsub: 'f',
        bounce: 'f',
        blocked: 'f',
        click: 't',
        spam: 't',
        open: 'f',
        queued: 'f',
        sent: 't',
        retailer: '4',
        summary: 'f' } }

I want to update prop, camp1, camp2. I want to pass it through a variable. So at one moment variable = camp1. I want to set campaignResponse.variable to obj. But I am getting a Syntax Error saying "Unexpected token . ".

User.findOneAndUpdate({email: res[i].email},{$set: {campaignSummary: temp, campaignResponse.varialble: obj}},{new: true},function(err ,result2){
                                        if(err)
                                            callback(err);
                                        else{
                                            console.log(result2);
                                            callback(err,result2);
                                        }
                                    })  

Please help me out here folks. Even suggestion of a better way to do this would be appreciated. Thanks.

Ankur Chavda
  • 173
  • 4
  • 17
  • 1
    You basically need to do something like `var update = { "$set": { "campaignSummary": temp } }; update['campaignResponse.' + variable] = obj;` Or something more ES6"ish" `{ "$set": { "campaignSummary": temp, ['campaignReponse.'+variable]: obj } }` But the approaches are shown in the linked duplicate. – Neil Lunn Jul 24 '17 at 10:03
  • @NeilLunn it worked, Thanks :D – Ankur Chavda Jul 24 '17 at 10:08

1 Answers1

1

I am sorry, I missed the part about it being dynamic...

Should be

var $set = {campaignSummary: temp};
$set["campaignResponse."+varialble] = obj;
User.findOneAndUpdate({email: res[i].email},{$set:$set},{new: true},function(err ,result2){
                                    if(err)
                                        callback(err);
                                    else{
                                        console.log(result2);
                                        callback(err,result2);
                                    }
                                })  

What you want, when using a key that is coming from a variable is to use it to concatenate the final string and then use it in the [] syntax to be able to define the correct property name.

Salketer
  • 14,263
  • 2
  • 30
  • 58
  • I tried doing that. It does not interpret variable as camp1 if I do that. Hence it adds a new field called "variable" inside campaignResponse. – Ankur Chavda Jul 24 '17 at 09:58