0

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
Rawle Juglal
  • 395
  • 4
  • 17

2 Answers2

0

I think you need to convert first _id to ObjectId. Refer this Convert string to ObjectID in MongoDB

Community
  • 1
  • 1
Parshuram Kalvikatte
  • 1,616
  • 4
  • 20
  • 40
0

The problem seems to be your query, the way it's trying to fetch the document, I would suggest to call findOne before update and see if you are really getting the intended document, For sure you must not be getting with the query.

Also am not sure the logic behind 'episode_number':{$gt:req.body.episode_number-1, $lt:req.body.episode_number+1}

why can't it be 'episode_number': req.body.episode_number

Rahul Kumar
  • 2,781
  • 1
  • 21
  • 29
  • Thanks for the comment. I updated the question to where I am now. I can get the document I wanted but you are correct I was not getting it before. It is adding an episode object into the episodes array but I can't figure out how to check and make sure that I'm not adding an episode object with a season_number and episode_number that already exist. The $gt: code was trying to check that the episode_number I provided in req.body was the next available episode number but I think now that wasn't good logic. I'm thinking I need to use $elemMatch and $ne but everything I've tried breaks the route. – Rawle Juglal Nov 03 '16 at 16:49
  • I think you want to add an object in the list when there is no such object but there is document. Now look at the other side it, what if the document itself is not there and what if document is there and the object is also there with same season number and episode number. I doubt that same update query will handle all the scenarios. As for the query , you are right you should use $elemMatch inside $not. – Rahul Kumar Nov 03 '16 at 17:44
  • There are various ways and probably none of them are all wrong, for example you can maintain one inner variable whose value will be the concatenation of season number and episode number, this will remove the use of $elemMatch. And then there is old school way, fetch the document , do all the logic in your application and save it. I know it's kind of boring but sometime keeping things simple is better. – Rahul Kumar Nov 03 '16 at 17:47