gpt4 book ai didi

node.js - 如何检查给定日期是否在 mongodb 中的两个日期之间?

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:14 24 4
gpt4 key购买 nike

{
"_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 数据库中的数据。我怎样才能只在任何地方找到数据 “状态”:“事件”, “型号名称”:“lumia”, “日期”:“19/04/2016”

以上是我的条件,只有我无法解决日期问题 这里给定的日期介于 fromdate 和 todate 之间,那么只有它应该给出结果

我需要检查给定日期(2016 年 4 月 19 日),该日期介于 fromdate 和 todate 之间,之后我需要检查“Status”:“Active”, “型号名称”:“lumia”,

我的 Controller :

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);

最佳答案

如果您想在 fromdatetodate 之间查找查询日期,并检查 modelnameStatus则可以遵循这个条件。

根据您给定的模型结构,您需要使用 (.) 表示法来检查您的日期,例如:Categories.mainmodels.fromdate并且需要使用$lte(小于或等于)和$gte(大于或等于)运算符.

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

关于node.js - 如何检查给定日期是否在 mongodb 中的两个日期之间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36712479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com