I've resolved the duplicate error and this route will add an episode object to the episodes array but I have not solved how to check in the query not only for the document ObjectId but also inside the episodes array and make sure there isn't a season_number and episode_number together that match the one I'm adding in the req.body.
This is my route
router.put('/api/shows/:id/episodes/add', function(req, res){
var setObject = req.body;
setObject.episode_id = new mongoose.Types.ObjectId;
Show.update({'_id':req.params.id}, {$push:{'episodes':setObject}}, function(err, doc){
if(err){console.log(err)}
else{res.json(doc)};
})
})
This is my database document
{
"_id" : ObjectId("58190ebffa6503f8d9e114cc"),
"title" : "Designated Survivor",
"poster" : "https://images-na.ssl-images-amazon.com/images/M/MV5BMTY5NzYzODU4N15BMl5BanBnXkFtZTgwNzA1MjUwMDI@._V1_.jpg",
"rated" : "TV-14",
"program_time" : 60,
"network" : "ABC",
"airs_on" : [
"Wednesday"
],
"streams_on" : [
"123Movies",
"Hulu Plus"
],
"genre" : [
"Drama"
],
"users" : [ ],
"episodes" : [
{
"_id":ObjectId('#############'),
"season_number" : 1,
"episode_number" : 1,
"title" : "Pilot",
"watched" : true
},
{
"season_number" : 1,
"episode_number" : 2,
"title" : "The First Day",
"watched" : true
},
{
"season_number" : 1,
"episode_number" : 3,
"title" : "The Confession",
"watched" : true
},
{
"season_number" : 1,
"episode_number" : 4,
"title" : "The Enemy",
"watched" : true
},
{
"season_number" : 1,
"episode_number" : 5,
"title" : "The Mission",
"watched" : true
},
{
"season_number" : 1,
"episode_number" : 6,
"title" : "The Interrogation",
"watched" : false
}
]
}
This is the call I'm using in postman
https://temp-crud-app-rawlejuglal.c9users.io/tv/api/shows/58190ebffa6503f8d9e114cc/episodes/add
-->body
title 'The Traitor'
watched false
season_number 1
episode_number 7