Reputation: 538
{
"_id": "581dc52e2c26be354164d528",
"name": "vipin",
"__v": 0,
"post": [
{
"like": "today",
"_id": "581dc52e2c26be354164d529",
"comment": [
{
"date": "today",
"username": "vipin",
"_id": "581dc52e2c26be354164d52a",
"detail": [
{
"time": "now",
"day": "saturday",
"_id": "581dc52e2c26be354164d52b"
}
]
}
]
}
]
},
i have a nested subdocument.i want to update comment .date.but i cant do this .i want to nested subdocument update but query is not work.
Test.update({"_id":"581dc52e2c26be354164d528","post._id":"581dc52e2c26be354164d529","comment._id":"581dc52e2c26be354164d52a" },{
"$set":{
"post.$.comment.0.date.0":"tommorow"
}
},function(err,data){
if(data){
res.json(data)
};
if(err){
res.json(err)
}
})
}
my query is now worked .plzz anybody suggest me .
Upvotes: 0
Views: 342
Reputation: 538
Okkk.we can update our nested subdocument in mongodb.this is our schema.
var Post = new mongoose.Schema({
name:String,
post:[{
like:String,
comment:[{
date:String,
username:String,
detail:{
time:String,
day:String
}
}]
}]
})
Now we can update our choosed field in mongoDb.if your document contains more than array of field like post,comment than it can be poor .but we can use many objects.like ......than we have no problem on updating.
post:[{
like:String,
comment:{
date:String,
username:String,
detail:{
time:String,
day:String,
address:{
street:String,
house:String
}
}
}
}]
})
so we have solution of first Schema is like this.our mongoDb query is working properly .so check this...
for first schema.if we have take more than one array in subdocument.
Test.update({"post._id":"58206a6aa7b5b99e32b7eb58"}, {$set:{ "post.$.comment.0.detail.time":"aajtk"}},
for second schema .if we have take more than objects.
db.tests.update({"post._id":ObjectId("58204e89cfee37f023a567bd")},{$set:{"post.$.comment.detail.time":"News"}})
Upvotes: 1
Reputation: 631
mongodb don't support positional operator matching nested arrays. check this issue it's still in open status: https://jira.mongodb.org/browse/SERVER-831
workaround : use aggregation and fetch required subdocument make modifications and update subdocument using update query.
Upvotes: 1