在mongodb中我如何检查给定日期是在两个日期之间?
{
"_id": {
"$oid": "asdf976"
},
"Categories": [{
"mainmodels": [{
"submodels": [{
"price": "2000",
"submodelname": "lumia021",
}, {
"price": "2000",
"submodelname": "lumia341",
}
],
"Status": "Active",
"modelname": "lumia",
"fromdate": "15/04/2016",
"todate": "29/04/2016"
}],
"brand": "nokia"
}],
"rank": "1",
"name": "first Mobile station"
}
{
"_id": {
"$oid": "asdf976"
},
"Categories": [{
"mainmodels": [{
"submodels": [{
"price": "2000",
"submodelname": "lumia a02",
}, {
"price": "2000",
"submodelname": "lumia trend",
}
],
"Status": "Active",
"modelname": "lumia",
"fromdate": "20/04/2016",
"todate": "21/04/2016"
}],
"brand": "nokia"
}],
"rank": "2",
"name": "Zahid station"
}
{
"_id": {
"$oid": "asdf976"
},
"Categories": [{
"mainmodels": [{
"submodels": [{
"price": "2000",
"submodelname": "trend 45",
}, {
"price": "2000",
"submodelname": "trendz",
}
],
"Status": "Active",
"modelname": "trend",
"fromdate": "10/04/2016",
"todate": "21/04/2016"
}],
"brand": "samsung"
}],
"rank": "3",
"name": "mobi cell station"
}
上面我添加了存储在mongodb数据库中的数据。
Status:Active,
modelname:lumia,
date:19/04/2016
Above i have added my data's which is stored in mongodb database . how can i find data only where ever "Status": "Active", "modelname": "lumia", "date":"19/04/2016"
以上是我的条件,日期的事情只有我无法解决
这里给定日期是从fromdate和todate之间,那么只应该给出结果
above is my condtion that date thing only i can't able to solve here given date is between fromdate and todate then only it should give the result
我需要检查给定日期(19/04/2016),这是从date和date之间,我需要检查状态:活动,
modelname :lumia,
i need to check given date (19/04/2016) which is between fromdate and todate after that i need to check "Status": "Active", "modelname": "lumia",
我的控制器:
exports.search = function(req, res) {
var condition = {
'modelname': 'lumia',
'Status':'Active'
};
Phone.find(condition).sort('-created').populate('user', 'displayName').exec(function(err, phones) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(phones);
}
});
};
架构:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var submodelSchema = {
submodelname: {type:String, required: false},
price: {type:String, required: false},
Remainingphones: {type:String, required: false},
Bookedphones: {type:String, required: false},
Numofphones: {type:String, required: false}
};
submodelSchema = 'new Schema('+ submodeleSchema +',{_id:true})';
var typeSchema = {
vtype: {type:String, required: false},
mainservices: {
Status: {type:String, required: false},
modelname : {type:String, required: false},
fromdate: {type: Date,default: Date.now},
todate: {type: Date,default: Date.now}
},
submodels: [submodelSchema], default:[]
};
typeSchema = 'new Schema('+typeSchema +',{_id:true})';
var PhoneSchema = new Schema({
rank: {
type: String,
default: '',
trim: true
},
name: {
type: String,
default: '',
trim: true
},
Categories: [typeSchema], default:[]
});
mongoose.model('Phone', PhoneSchema);
如果您想查找查询日期 fromdate
和 todate
,还要查看 modelname
和状态
然后可以遵循此条件。
If you want to find your query date between fromdate
and todate
and also check modelname
and Status
then can follow this condition.
根据您给定的模型结构,您需要使用点(。
)符号来检查你的日期,如: Categories.mainmodels.fromdate
,需要使用 $ lte
(小于或等于)和 $ gte
(大于或相等的)运算符。
Aaccording to your given model structure you need to use dot (.
) notation to check your date like: Categories.mainmodels.fromdate
and need to use $lte
(less than or equal) and $gte
(greater than or equal) operator.
var queryDate = '19/04/2016'; // or like: var queryDate = req.query.date; or as you sent
var condition = {
'modelname': 'lumia',
'Status':'Active',
'Categories.mainmodels.fromdate': {$gte: queryDate },
'Categories.mainmodels.todate': {$lte: queryDate }
};
Phone.find(condition).sort('-created').// rest of code