How to extend Meteor.users schema by meteor-collection2 package

Hello all. I have extended users schema, but can’t understand how update or retreve data from it correctly.

User profile

/// --- User Profile --- ///
UserProfile = new Mongo.Collection("userprofile");
Schema.UserProfile = new SimpleSchema({
    userName: {
        type: String,
        optional: true
    },
    userGender: {
        type: String,
        allowedValues: ['Male', 'Female'],
        optional: true
    },
    userAge: {
        type: Number,
        optional: true,
        regEx: /^[0-9]{2}$/
    },
    userWeight: {
        type: Number,
        decimal: true,
        optional: true
    },
    userGrowth: {
        type: Number,
        optional: true
    },
    userExpirience: {
        type: Number,
        optional: true
    },
    userAvatarId: {
        type: String,
        optional: true
    },
    profileComplete: {
        type: Boolean,
        optional: true
    }
});
UserProfile.attachSchema(Schema.UserProfile);

User

/// --- User --- ///
Schema.User = new SimpleSchema({
    username: {
        type: String,
        regEx: /^[a-z0-9A-Z_]{3,15}$/
    },
    emails: {
        type: [Object]
    },
    "emails.$.address": {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    "emails.$.verified": {
        type: Boolean
    },
    createdAt: {
        type: Date
    },
    profile: {
        type: Schema.UserProfile,
        optional: true
    },
    services: {
        type: Object,
        optional: true,
        blackbox: true
    },
    // Add `roles` to your schema if you use the meteor-roles package.
    // Option 1: Object type
    // If you specify that type as Object, you must also specify the
    // `Roles.GLOBAL_GROUP` group whenever you add a user to a role.
    // Example:
    // Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP);
    // You can't mix and match adding with and without a group since
    // you will fail validation in some cases.
    roles: {
        type: Object,
        optional: true,
        blackbox: true
    },
    // Option 2: [String] type
    // If you are sure you will never need to use role groups, then
    // you can specify [String] as the type
    roles: {
        type: [String],
        optional: true
    }
});
Meteor.users.attachSchema(Schema.User);

When I try to update data it destroy the data of user and collection contain only id

Meteor.users.update({_id: userId}, {$set: {userAge: 30}}, {validate: false}); 

Do anybody can help or maybe give link to good article or example?
Thanks.

I think that you are doing wrong, because the entry that you want edit are on profile schema, so try this:

Meteor.users.update({_id: userId}, {$set: {profile: {userAge: 30}} }, {validate: false});
2 Likes

Thank you @jadsonlourenco
It is working but I have one little problem :slight_smile: When I make update for another profile property it is destroy previous. For example, if I update userAge and after userGender it is removes userAge and only userGender will be in collection.

1 Like

Oh I get this problem on begin too, haha. Well learn a bit of MongoDB operations: http://docs.mongodb.org/manual/reference/operator/update/set/

Try this with your example:

Meteor.users.update({_id: userId}, {$set: {"profile.userAge": 30}}, {validate: false});

I think will update only this field, tell me please, I don’t have an easy way to test it now.

1 Like

Yes. It work. Thanks!

1 Like