简体   繁体   English

同一模式下的猫鼬查询

[英]Mongoose query within the same schema

Is it possible to perform a query within the same schema? 是否可以在同一模式下执行查询? For example, If I have a schema which has 2 date fields, and I need to find the data where one Date field is greater than the other. 例如,如果我有一个包含2个日期字段的架构,并且我需要查找一个日期字段大于另一个日期字段的数据。 This is my schema and code sample. 这是我的架构和代码示例。

 var Schema = mongoose.Schema; var someSchema = new Schema({ someId : { type: String, default: '' ,index:true, unique: true }, userName : { type: String, default: ''}, fullName : { type: String, default: '' }, created : {type: Date, default:''}, lastUpdated : {type: Date, default:''}, primaryGroupId : {type:String,default:''}, nextHearing : {type: Date, default:''}, status : {type:String,default:'open'}, }); mongoose.model('Problem', someSchema); 

The below code is my query. 下面的代码是我的查询。

 var problemModel = mongoose.model('Problem'); var today = Date.now(); problemModel.find({$and:[{'nextHearing':{$lte: today}},{'nextHearing':{$gte : 'lastUpdated'}}]},function(err, result){ 

When I run the program, I get the following error 运行程序时,出现以下错误

{ message: 'Cast to date failed for value "lastUpdated" at path "nextHearing"', name: 'CastError', type: 'date', value: 'lastUpdated', path: 'nextHearing' } {消息:路径“ nextHearing”上的值“ lastUpdated”的最新发布失败,名称:“ CastError”,类型:“ date”,值:“ lastUpdated”,路径:“ nextHearing”}

new Date() returns the current date as a Date object. new Date()返回当前日期作为Date对象。 The mongo shell wraps the Date object with the ISODate helper. mongo shell使用ISODate帮助程序包装Date对象。 The ISODate is in UTC. ISODate使用UTC。

so you may need to change: 因此您可能需要更改:

var today = Date.now();

to

var today = new Date().toISOString();

Also, take a look at this 另外,看看这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM