with the following mongo object,
how would I findById of a user whose _id is '590ac6b7663350948be1c085' to then find in their friends array an object whose id is '590ac6ac663350948be1c083' and then set its status property to 1?
_id: 590ac6b7663350948be1c085,
username: 'someusername',
friends:
[{
id: 590ac6ac663350948be1c083,
status: 0,
_id: 590ace171aa0aeb58f798466
}]
I've tried
User.findOne({'_id': '590ac6b7663350948be1c085'}).then(user => {
let friend = user.friend.id('590ac6ac663350948be1c083');
friend.status = 1;
return user.save();
});
for now the schema is rather simple
var UserSchema = new Schema({
username : String,
friends :
[{
id: { type: Schema.Types.ObjectId, ref: 'User'},
status: Number
}]
});
or is a better approach to find the _id of that particular friend object and update that then? if so, how would I do that as this query returns the entire user object
User
.findOne({ 'friends._id' : '590ace171aa0aeb58f798466' })
.then(user => {
console.log(user)