Node.js 更新父子数据nodejs mongodb
我是nodejs的mongodb新手。我想更新评论数据。这是我的文档Node.js 更新父子数据nodejs mongodb,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我是nodejs的mongodb新手。我想更新评论数据。这是我的文档 { "__v": NumberInt(0), "_id": ObjectId("565443b1172e19d51f98b0ed"), "address": "tohana", "comments": [ { "_id": ObjectId("5654455fe088d89c20736e3c"), "comm
{
"__v": NumberInt(0),
"_id": ObjectId("565443b1172e19d51f98b0ed"),
"address": "tohana",
"comments": [
{
"_id": ObjectId("5654455fe088d89c20736e3c"),
"comment": "good man",
"uemail": "dinesh@gmail.com",
"uname": "dinesh"
},
{
"_id": ObjectId("565445e471dce6ca20705a84"),
"comment": "nice person",
"uemail": "kr@gmail.com",
"uname": "krishan"
},
{
"_id": ObjectId("5654460e7aa73bec2064060e"),
"comment": "bad person",
"uemail": "Rai",
"uname": "Rahul"
}
],
"email": "nishantg@ocodewire.com"▼,
"name": "Nishant"
}
我在前端使用Angular js,在后端使用nodeJs。这是我的密码:
app.post('/commentsSave/:id', function(req, res) {
var id = req.params.id; // userId
var input = JSON.parse(JSON.stringify(req.body)); //commentData
var commentId = input.id; //commentId
var name = input.name;
var email = input.email;
var comment = input.comment
res.send({data:id,data2:input});
})
你能添加你的mongoose模型吗?你的意思是你只想更新现有数据中的注释?我已经添加了mongoose模型。我只想更新注释@prasundo您想使用mongoose或mongodb本机驱动程序吗?
app.post('/commentsSave/:id', function(req, res) {
var id = req.params.id; // userId
var input = JSON.parse(JSON.stringify(req.body)); //commentData
var commentId = input.id; //commentId
var name = input.name;
var email = input.email;
var comment = input.comment
Users.update({'comments._id': input.id},
{
$set: {
'comments.$.name': name,
'comments.$.email': email,
'comments.$.comment': comment
}
},function(error, result){
if(error){
console.log(error);
}
else{
console.log(result);
}
})
}