Correct way to update user profile? [SOLVED]

I’m a bit confused…
When a user signs up, i’ve added a couple of fields to make a user profile containing name and surname.
After they’ve logged in, the user can update the fields manually by calling the following function:

Meteor.users.update( { _id: Meteor.userId() }, { $set: {profile: {name 'yogi' surname: 'yo'}}} );

However if the user would only like to update one field, the other field would be overwritten and no longer exist in the collection

//surname does no longer exist
Meteor.users.update( { _id: Meteor.userId() }, { $set: {profile: {name 'yogi'}}} );

Is it suppose to be like this or am I missing something?
I understand that $set does what it does but isn’t the whole idea of an update function to only affect the fields you wanted it to?

Try to do this:

Meteor.users.update({_id: Meteor.userId()}, {$set: {"profile.name": "yogi"}});

See this link in the MongoDB for more details.

3 Likes

That did the trick! I did go with profile.name earlier but without the “”, i’ll need to read up on why it has to be within “”-marks. Is it a mongo specific thing?

This is using MongoDB’s dot notation for querying inside documents or arrays. See this link on dot notation.