Answer the question
In order to leave comments, you need to log in
How to push an object into an array nested in an object, mongo?
There is a mongo model
var userSchema = new Schema({
name:{
required: true,
type: String
},
email:{
unique: true,
required: true,
type: String
},
password:{
required: true,
type: String
},
admin: {
type: Boolean,
default: false
},
posts: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
}],
friends: { // object
incomeRequests:[{ //array in object
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
}],
outcomeRequests:[{
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
}],
friends: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
}]
}
});
User.update({_id: accountId}, {$push:{'posts':postObj}}, function(err, post){
if(err){
console.log(err)
res.end()
}else{
console.log(post)
res.end()
}
});
Answer the question
In order to leave comments, you need to log in
Everything is simple, it was necessary to do as with ordinary nested arrays{$push:{'friends.incomeRequests':obj}}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question