node.js - How to I add push data to an array in Mongoose DB on create method? -
i'm trying initialize mongoose schema objects in array in create method.
my schema looks following:
var imageschema = new mongoose.schema({ url: {type:string}, text: {type:string} }); module.exports = todo.model('todo', new mongoose.schema ({ name : {type : string, default: ''}, dataarr : [imageschema] })); and route looks like:
app.post('/api/todos', function(req, res) { todo.create({ name : req.body.name, }, { $push : { dataarr : { url : 'foo' , text : 'bar'} } }, function(err, todo) { if (err) { res.send(err); } }); }); any appreciated.
i'm pretty sure create method doesn't accept second parameter can provide $push.
since creating object, set array naively:
app.post('/api/todos', function(req, res) { todo.create({ name : req.body.name, dataarr : [{ url : 'foo' , text : 'bar'}] }, function(err, todo) { if (err) { res.send(err); } }); });
Comments
Post a Comment