There is a modal called Movie having information about a movie.
Movie Modal
var mongoose = require('mongoose');
var movieSchema = new mongoose.Schema({
m_tmdb_id: {
type: Number,
unique: true,
index: true
},
m_backdrop_path: {
type: String,
},
m_budget: {
type: Number,
},
m_homepage: {
type: String
},
m_imdb_id: {
type: String,
},
m_original_language: {
type: String
},
m_original_title: {
type: String
},
m_poster_path: {
type: String
},
m_poster_key: {
type: String
},
m_release_date: {
type: Date
},
m_revenue: {
type: Number
},
m_runtime: {
type: Number
},
m_title: {
type: String
},
m_genres: {
type: Array
},
created_at: {
type: Date
},
updated_at: {
type: Date,
default: Date.now
}
});
var MovieModel = mongoose.model('Movie', movieSchema);
module.exports = {
movie: MovieModel
}
I need to select 10 items in each query [Pagination] from the collection Movie with different conditions.I have added 3 condition in my API[Based on gener name, release date, language].
Js Code
router.post('/movies', function(req, res, next) {
var perPage = parseInt(req.query.limit);
var page = req.query.page;
var datefrom = new Date();
var dateto = new Date();
var generNames = req.body.generNames;
dateto.setMonth(dateto.getMonth() - 2);
var queryOptions = {
$and: [{
'm_release_date': {
$lte: datefrom,
$gte: dateto
}
}, {
"m_genres.name": {
$in: generNames
}
}, {
'm_original_language': 'en'
}, ]
};
Movie
.find(queryOptions)
.select('_id m_tmdb_id m_poster_path m_original_title')
.sort('-m_release_date')
.limit(perPage)
.skip(perPage * page)
.exec(function(err, movies) {
if (movies) {
return res.status(200).json(movies);
}
}).catch(function(error) {
return res.status(500).json(error);
});
});
I need to add one more condition ,the condition is select items from the collection Movie that having release date [m_release_date] from the set of years [ex: 2003,2004,2010 etc].How can i do this?enter code here
Example:
Movie Collection
[
{
"_id": "59420dff3d729440f200bccc",
"m_tmdb_id": 453651,
"m_original_title": "PIETRO",
"m_poster_path": "/3sTFUZorLGOU06A7P3XxjLVKKGD.jpg",
"m_release_date": "2017-07-14T00:00:00.000Z",
"m_runtime": 8,
"m_genres": [{
"id": 18,
"name": "Drama"
}]
},
{
"_id": "594602610772b119e788edab",
"m_tmdb_id": 425136,
"m_original_title": "Bad Dads",
"m_poster_path": null,
"m_release_date": "2017-07-14T00:00:00.000Z",
"m_runtime": 0,
"m_credits_cast": [],
"m_genres": [{
"id": 35,
"name": "Comedy"
}]
},
{
"_id": "59587747d282843883df755e",
"m_tmdb_id": 364733,
"m_original_title": "Blind",
"m_poster_path": "/cXyObe5aB63ueOndEXxXabgAvIi.jpg",
"m_release_date": "2017-07-14T00:00:00.000Z",
"m_runtime": 105,
"m_genres": [{
"id": 18,
"name": "Drama"
}]
},
{
"_id": "595d93f9c69ab66c4f48254f",
"m_tmdb_id": 308149,
"m_original_title": "The Beautiful Ones",
"m_poster_path": "/kjy1obH5Oy1IsjTViYVJDQufeZP.jpg",
"m_release_date": "2017-07-14T00:00:00.000Z",
"m_runtime": 94,
"m_genres": [{
"id": 18,
"name": "Drama"
}]
},
{
"_id": "59420de63d729440f200bcc7",
"m_tmdb_id": 460006,
"m_original_title": "Черная вода",
"m_poster_path": "/kpiLwx8MGGWgZMMHUnvydZkya0H.jpg",
"m_release_date": "2017-07-13T00:00:00.000Z",
"m_runtime": 0,
"m_genres": []
},
{
"_id": "594602390772b119e788eda3",
"m_tmdb_id": 281338,
"m_original_title": "War for the Planet of the Apes",
"m_poster_path": "/y52mjaCLoJJzxfcDDlksKDngiDx.jpg",
"m_release_date": "2017-07-13T00:00:00.000Z",
"m_runtime": 142,
"m_genres": [{
"id": 28,
"name": "Action"
}
]
}
]
API Request