0

Assuming the following document structure, how is it possible to use a variable to define the indicated key? Values could be observational, surreal, physical, other.

If an object needs to be used to create the update, please show how. If it would be better to restructure the document then suggestions are welcome.

    Comedians.update(
      { "id":row[0]},
      { 'id':row[0],
        'surname':row[1],
        'firstName':row[2],
        'observational':{ //<--key generated dynamically (observational/ surreal/ physical)
          'routines':{
            'year1':{
              'club1':row[6],
              'club2':row[8],
              'club3':row[10]
            },
            'year2':{
              'club1':row[14],
              'club2':row[16],
              'club3':row[18]
            },
            'year3':{
              'club1':row[20],
              'club2':row[22],
              'club3':row[24]
            }
          },
          'jokes':{
            'year1':{
              'club1':row[7],
              'club2':row[9],
              'club3':row[11]
            },
            'year2':{
              'club1':row[13],
              'club2':row[15],
              'club3':row[17]
            },
            'year3':{
              'club1':row[19],
              'club2':row[21],
              'club3':row[23]
            }
          }, 
        } 
      },
      {upsert: true},
      function(error,result){
        if ( error ) console.log ( error ); 
        if ( result ) console.log ( result );
      });

Other answers on SO do not utilize embedded documents and this appears to be an issue here.

mvanio
  • 505
  • 3
  • 15
  • Please update your question with what you have tried – challett Oct 05 '15 at 18:12
  • 3
    @mvanio This seems like a duplicate of [this](https://stackoverflow.com/questions/18577825/when-update-nested-collection-in-meteor-how-to-pass-variable-for-the-update-pat) and [this](https://stackoverflow.com/questions/22568673/updating-a-specific-element-in-an-array-with-mongodb-meteor). Do those answer your question or you asking something else? – David Weldon Oct 05 '15 at 18:14
  • @DavidWeldon Tried those, think I'm not understanding how to apply the solution with an embedded document. Or that perhaps the document structure is off. Nice blog BTW, have referred to your 'common mistakes' several times. Very useful. – mvanio Oct 05 '15 at 18:17

1 Answers1

0

This is basic javascript. You need to create the object to use for the update using code, rather than writing it out as a constant. Here is one way:

var newObj = { 
    'id':row[0],
    'surname':row[1],
    'firstName':row[2],
};
var myVariable = "observational";  //<--key generated dynamically (observational/ surreal/ physical)
newObj[myVariable] = {
    'routines':{
        'year1':{
            'club1':row[6],
            'club2':row[8],
            'club3':row[10]
        },
        'year2':{
            'club1':row[14],
            'club2':row[16],
            'club3':row[18]
        },
        'year3':{
            'club1':row[20],
            'club2':row[22],
            'club3':row[24]
        }
    },
    'jokes':{
        'year1':{
            'club1':row[7],
            'club2':row[9],
            'club3':row[11]
        },
        'year2':{
            'club1':row[13],
            'club2':row[15],
            'club3':row[17]
        },
        'year3':{
            'club1':row[19],
            'club2':row[21],
            'club3':row[23]
        }
    }, 
};

Comedians.update(
    { "id":row[0]},
    newObj,
    {upsert: true},
    function(error,result){
        if ( error ) console.log ( error ); 
        if ( result ) console.log ( result );
    }
);
Christian Fritz
  • 20,641
  • 3
  • 42
  • 71