【发布时间】:2018-10-06 16:09:55
【问题描述】:
我正在尝试使用 mongoose 更新我的收藏。
这是我的架构:
var FamilySchema = mongoose.Schema({
construction: {
type: Array,
default: [
["foundry", 0],
["farm", 0],
["sawmill", 0]
]
}
});
这是我的代码:
app.put('/construction/updateConstruction/:id', (req, res) => {
let id = req.params.id;
Family.findById(id, (err, familiaDB) => {
if (err) {
return res.status(500).json({
ok: false,
err
});
}
if (!familiaDB) {
return res.status(400).json({
ok: false,
err
});
}
// I want to update the value of the posicion 0 in the array.
familiaDB.construction[0][1] = 1;
familiaDB.save();
console.log(familiaDB);
});
});
发出请求后在 console.log 中的结果:
Escuchando puerto: 3000
Base de datos ONLINE
{ state: true,
construction:
[ [ 'foundry', 1 ],
[ 'farm', 0 ],
[ 'sawmill', 0 ],
_id: 5bb8d69c604625211c572ada,
__v: 0 }
在 console.log 中一切都很好并且更新了,但在我的数据库中它没有更新。我在robomongo中检查了很多次,从来没有更新过。
【问题讨论】:
标签: arrays node.js database mongodb mongoose