目前我在使用mongoose为nodejs更新现有JSON数据时遇到问题。这是我的JSON数据:
{
"_id" : ObjectId("59fb4d91fa90e127d41ed7f5"),
"heroes": [
{
"name": "antimage",
"id": 1,
"localized_name": "Anti-Mage",
"_id" : ObjectId("59fc7230728a3e28203151a0")
},
{
"name": "axe",
"id": 2,
"localized_name": "Axe",
"_id" : ObjectId("59fc7230728a3e28203111a4")
}
]
}
这是我的更新请求:
// update a hero in the db
router.put('/heroes/:id', function (req, res, next) {
//var id1 = req.params.id1;
//var id = req.params.id;
Hero.update({ _id: '59fb4d91fa90e127d41ed7f5', 'heroes._id': req.params.id },
{
$push: {
'heroes.$.name': req.body.name,
'heroes.$.id': req.body.id,
'heroes.$.localized_name': req.body.localized_name
}
}, { upsert: true }, function (err, docs) {
res.json(docs);
console.log(docs);
});
})
它在控制台中给我这个:
{
"ok": 0,
"n": 0,
"nModified": 0
}
这让我无法入睡,
顺便感谢你的帮助。