I am trying to save a task to a list of tasks with mongoose and MongoDB. I want to save it redundantly in the tasks collection and in the corresponding list document as embedded document.
It works fine but one little thing: The list's embedded documents don't have their objectIds. But I need them in order to connect them logically with the documents in the tasks-collection.
My Schemas:
var TaskSchema = new Schema({
_id: ObjectId,
title: String,
list: ObjectId
});
var Task = mongoose.model('task', TaskSchema);
var ListSchema = new Schema({
_id: ObjectId,
title: String,
tasks: [Task.schema]
});
var List = mongoose.model('list', ListSchema);
My controller/router:
app.post('/lists/:list_id/tasks', function(req, res) {
var listId = req.params.list_id;
// 1. Save the new task into the tasks-collection.
var newTask = new Task();
newTask.title = req.body.title;
newTask.list = listId;
console.log('TaskId:' + newTask._id); // Returns undefined on the console!!!
newTask.save(); // Works fine!
// 2. Add the new task to the coresponding list.
list.findById(listId, function(err, doc){
doc.tasks.push(newTask);
doc.save(); // Saves the new task in the list but WITHOUT its objectId
});
res.redirect('/lists/' + listId)
});
Can I use mongoose a different way to achieve that? Or do I have to save the task and then query it before saving it in the list?
Thank you for advise :-)