无法创建正确的查询以达到mongodb文档

时间:2016-11-02 06:35:00

标签: node.js mongodb express mongoose crud

我需要将mongo shell命令转换为快速路径中正确的mongoose更新。

网址中的:id是我文档中的ObjectId#。 req.body将包含一个对象,其中包含titleseason_numberepisode_numberwatched的键/值。我以为我只是替换了这部分mongo shell查询

{ 'season_number': 1, 'episode_number': { $gt: 4, $lt: 6 },

{
    'season_number': req.body.season_number, 
    'episode_number': {
        $gt: req.body.episode_number - 1, 
        $lt: req.body.episode_number + 1
    } 
}
查询中的

但是没有找到该文档。

路线

router.put('/api/shows/:id/episodes/add', function(req, res){
    var query = {
     /* 
        I've tried many things but my query never returns the document 
        to update so I am pretty sure the problem is here
     */
    }

    var setObject = {
        $push:{
            'episodes':req.body
        }
    }

    TV.update(query, setObject,  function(err, results){
        if(err){console.log(err)}
        else{res.json(results)};
    })
})

Mongo Shell文档

{
    "_id" : ObjectId("581972b7b04acfc99b4dae0f"),
    "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"
    ],
    "episodes" : [
            {
                    "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
            },
            {
                    "title" : "The Interrogation",
                    "season_number" : 1,
                    "episode_number" : 6,
                    "watched" : false
            }
    ],
    "test" : "gt four less than 6"
}

添加第6集的Mongo Shell Command

db.tvShows.findOneAndUpdate(
    {
        $and: [
            { '_id': ObjectId('581972b7b04acfc99b4dae0f') },
            { 'episodes': {
                $elemMatch: {
                    'season_number': 1, 
                    'episode_number': { $gt: 4, $lt: 6 }
                } 
            } }
        ]
    },
    {
        $push: {
            'episodes': {
                'title': 'The Interrogation', 
                'season_number': 1, 
                'episode_number': 6, 
                watched: false
            }
        }
    }
)

0 个答案:

没有答案