These are chat archives for Automattic/mongoose

Aug 2016
Aug 30 2016 19:35

Hello, I want to update a value in an array of objects. My user model is :

    _id: 57be1bbed87abd6e221b3627,
    email: '',
    password: '$2a$10$fxKkZxuu9w8H9osxdic8vuinHHxzFpPk/gW3CzEJxo4yzrYjjs5hq',
    __v: 121,
    channels: [ 
            id: 'red',
            name: 'Red'
            id: 'blue',
            name: 'Blue'

For example, I want update name for the red id. I tried this :

user.update({'': 'red'}, {'$set': {
    'channels.$.name': 'new name'
}}, function(err){

But it doesn't work. I found this solution on this post

Aug 30 2016 19:42
You need to $elemMatch the correct element
Varun Jayaraman
Aug 30 2016 20:53
Getting a weird bug in mongoose@4.5.10 and v.4.5.2 where an embedded array (without its own schema) triggers an error in mongoose/lib/types/documentarray.js line 182, stating that doc.toObject is not a function, but ONLY when i use document#save and not Model#findByIdAndUpdate. I get that doc.toObject is not a function because the subdocs in the embedded array don't have their own schema, but has anyone had this issue before?
sorry line 181, not line 182
Varun Jayaraman
Aug 30 2016 22:06
This message was deleted
deleted my comment, but i think i'm right... if you set a new element in an array through its index rather than through .push, it doesn't insert it into the array as a document, but as a POJO