Reputation: 5841
I have the following code that performs basic updates on a Mongo document. The problem is that after I run the /stop/:id
route, the startDate
field gets deleted from the embedded document. How can I make sure it stays there after the endDate
and started
fields are updated?
router.get('/start/:id', function(req,res){
var collection = db.get('Activity');
collection.update({
_id: req.params.id
},
{
$set: {
"runtime": {
started: true,
startDate: new Date(),
endDate: null
}
}
}, function(err, activity){
if (err) throw err;
res.json(activity);
});
});
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()
}
}, function(err, activity){
if (err) throw err;
res.json(activity);
});
});
Upvotes: 0
Views: 49
Reputation: 5841
The following is a working code for this:
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()
}
},
function(err, activity){
if (err) throw err;
res.json(activity);
});
});
Upvotes: 0
Reputation: 3138
Trh this using $addToSet
router.get('/stop/:id', function(req,res){
var collection = db.get('Activity');
collection.update({
_id: req.params.id
},
{
$addToSet: {
"runtime.started": false,
"runtime.endDate": new Date()
}
}, function(err, activity){
if (err) throw err;
res.json(activity);
});
});
Upvotes: 1