I am trying to do a query that seems to be basic on the surface. However, I cannot seem to do it efficiently and elegantly. Namely, I have the following document:
{
id:
runtime: {
start: Date
end: Date
total: number
}
events: [runtime1, runtime2, ...]
}
Upon a certain API call, I would like to set the runtime.end
to the current date and then push what is contained in the runtime object into the events array.
Here is the code I have been trying to use for it:
router.get('/stop/:id', function(req,res){
var collection = db.get('Activity');
collection.update({
_id: req.params.id
},
{
$set: {
"runtime.started": false,
"runtime.endDate": new Date()
},
$push: {events: {
startDate: new Date,
endDate: new Date
}
}
},
function(err, activity){
if (err) throw err;
res.json(activity);
});
});
Any idea how this can be done? The code above seem to do nothing, except to edit the runtime
object.
P.S. I found several questions and answers circa 2010 - 2012 that stated that this could not be done. Have things changed since then?