0

I have a schema that looks like this:

var userSchema = mongoose.Schema({
    facebook         : {
        id           : String,
        token        : String,
        email        : String,
        name         : String
    },
    twitter          : {
        id           : String,
        token        : String,
        displayName  : String,
        username     : String
    },
    google           : {
        id           : String,
        token        : String,
        refreshToken : String,
        email        : String,
        name         : String
    },
    slack            : {
        id           : String,
        token        : String,
        teamId       : String,
        name         : String
    }

});

And i want to update google's access token given the refreshToken. How can i achieve that?

I tried this but it doesnt seem to work

       UserModel.update(
                { 'google.refreshToken': refreshToken },
                {token: 'asdf'},
                {multi: false},
                function (err, raw) {
                    if (err) {
                        console.log('Error log: ' + err)
                    } else {
                        console.log("Token updated: " + raw);
                    }
                }
        );
Manos
  • 1,471
  • 1
  • 28
  • 45

1 Answers1

1

I think you've got it the other way round:

UserModel.update(
    { "google.refreshToken": refreshToken },
    { "$set": { "google.token": "asdf" } },
    function (err, raw) {
        if (err) {
            console.log('Error log: ' + err)
        } else {
            console.log("Token updated: " + raw);
        }
    }
);
chridam
  • 100,957
  • 23
  • 236
  • 235
  • 1
    is $set mandatory here? On the official documentation i cant find something about it. http://mongoosejs.com/docs/api.html#model_Model.update – Manos Oct 04 '16 at 11:53