I am trying to push an element to an array in mongoose. I am doing it with update and $push. But it is not updating it in the database. This is my code. routes.js:
var Chooser = require('../chooser');
var appRouter = function(app) {
app.put("/chooser/:id", function(req, res) {
console.log("ID: ", req.params.id);
if (!req.body.toFollow) {
return res.send({"status": "error", "message": "The account that you want to follow is needed"});
}
else {
console.log("Cuenta: ", req.body.toFollow);
Chooser.update({_id: req.params.id}, {$push: {accounts: {"name": "foo", "idAccount": 123456}}});
return res.send({"status": "ok"});
}
});
}
This is my mongoose schema. Chooser.js:
var mongoose = require('mongoose');
var chooserSchema = mongoose.Schema({
_id: Number,
accounts: [{name: String, idAccount: Number}]
}, { _id: false });
var Chooser = mongoose.model('Chooser', chooserSchema);
module.exports = Chooser;