1

I have been stuck on this Meteor problem for a while. I am trying to update a nested array within a document in mongodb. However it doesnt seem to update the document at all. Also, there were no error messages emitted which left me confused.

Document structure:
     |----_id
     |----name
     |----members (Array)
          |---- [0]
               |----history (Array)
                     |----[0]={type:2,startdate:010304,enddate:120314}
                     |----[1]={type:1,startdate:140904,enddate:150914}
                     |----[2]={type:3,startdate:241204,enddate:291214}
                     |----!!!!!I WANNA ADD THE ENTRY HERE !!!!!!!!!!
               |----state
          |---- [1]
               |----history (Array)
                     |----[0]={type:2,startdate:010304,enddate:120314}
               |----State

This is the code

hisobj = {'type':type, 'startdate':startdate,'enddate':enddate};

parstr = "members["+insertmemberid+"].history";

Groups.update({"_id":Session.get("groupid")},{$addToSet:{parstr:{$each: [hisobj]} } });

Any advice would be greatly appreciated.

2 Answers 2

1

Inside your update function, parstr is a javascript identifier. It is being used as the property name without being parsed. It has the same effect as using the string "parstr". To use the value of a variable as a property name, you must use the array notation. So create an object to be used as the value of the addToSet operation and set a property there using the value of parstr.

hisobj = {'type':type, 'startdate':startdate,'enddate':enddate};

parstr = "members." + insertmemberid + ".history";

// create an object to be used as the value of the addToSet operation
var addToSetValue = {};
// the value stored in parstr will be used here
addToSetValue[parstr] = hisobj;

Groups.update({"_id":Session.get("groupid")}, {$addToSet: addToSetValue}}, function (error) {
  if (error) {
    console.log(error);
  }
});

Notice that inside parstr you need to use dot notation, as Hubert OG said. That's a different situation. We're just creating the string that mongodb will use internally to do it's magic.

Also, the callback as the last argument of the update call will let you see a possible error.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Kako! I learnt something new. However when I ran my code there was still no updates to my mongodb document. Is there anyway to print any error messages that arises from the update function? Thanks
Hi, 300poundcow. I edited the answer to show how you can see a possible error message.
0

Try the dot notation instead of brackets:

parstr = "members."+insertmemberid+".history";

Assuming insertmemberid === 0.

Also, since you use the $addToSet operator, make sure that the object you add to the array is not equal to another that's already there.

1 Comment

Right, I overlook that you also use the string in the wrong way. +1 for @Kako for pointing that out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.