mongoose数据范围查询不起作用

我有一个像这样的search方法。 (我也有很多其他代码做不同types的search,但为简单起见我省略了这些代码)

var mongoose = require('mongoose'); exports.searchWithParams = function (modal, req, res, next) { console.log(JSON.stringify(req.body)); var body = req.body; var orConditions = []; var andConditions = []; for (var i = 0; i < body.searchParms.length; i++) { var param = body.searchParms[i]; switch (modType) { case 'and': andConditions.push(param); break; case 'or': orConditions.push(param); break; default : throw new Error('Invalid mod type: ' + modType) } } var transalatedOrConditions = translateConditions(orConditions); var transalatedAndConditions = translateConditions(andConditions); var finalConditions = {}; if (transalatedOrConditions.length > 0) { finalConditions['$or'] = transalatedOrConditions; } if (transalatedAndConditions.length > 0) { finalConditions['$and'] = transalatedAndConditions; } var query = modal.find(finalConditions); query.exec(function (err, results) { if (err) return next(err); if (!results) return res.json(401); res.json(results); }); } function translateConditions(conditions) { var translatedConditions = []; for (var i = 0; i < conditions.length; i++) { var condition = conditions[i]; var fieldName = condition.name; var fieldValue = condition.value; var searchType = condition.searchType; var dataType = condition.dataType; var translatedCondition = {}; switch (searchType) { case 'range' : switch (dataType) { case 'date': if (!condition.minValue || !condition.maxValue) { throw new Error('missing minValue and maxValue for range query') } translatedCondition[fieldName] = {$gt: new Date(condition.minValue), $lt: new Date(condition.maxValue)}; translatedConditions.push(translatedCondition); break; default : throw new Error('Invalid datatype: ' + dataType) } break; default : throw new Error('Invalid search type: ' + searchType); } } return translatedConditions; } 

我给这样一个search参数

 { "searchParms": [{ "name": "topics.modules.classes.startTime", "minValue": "2014-12-31", "maxValue": "2015-01-31", "searchType": "range", "dataType": "date", "modType": "and" } ] } 

查询执行时,它生成的条件是这样的

 {"$and":[{"topics.modules.classes.startTime":{"$gt":"2014-12-31T00:00:00.000Z","$lt":"2015-01-31T00:00:00.000Z"}}]} 

如果我只是在mongo控制台执行相同的查询,我会得到结果

  db.subjects.find({"$and":[{"topics.modules.classes.startTime":{"$gt":"2014-12-31T00:00:00.000Z","$lt":"2015-01-31T00:00:00.000Z"}}]}); { "_id" : ObjectId("55538211864ff2daf3dfc504"), "name" : "math", "topics" : [ { "name" : "math topic", "startTime" : "", "endTime" : "", "fee" : "", "modules" : [ { "name" : "math module", "startTime" : "", "endTime" : "", "fee" : "", "classes" : [ { "name" : "math class", "startTime" : "2015-01-01T20:59:00.000Z", "endTime" : "2015-01-01T20:59:00.000Z", "fee" : 300 } ] } ] } ], "teacher" : ObjectId("5552de1756c7e627ebceb1af") } { "_id" : ObjectId("5553832d864ff2daf3dfc505"), "name" : "math1", "topics" : [ { "name" : "math1 topic", "startTime" : "", "endTime" : "", "fee" : "", "modules" : [ { "name" : "math1 module", "startTime" : "", "endTime" : "", "fee" : "", "classes" : [ { "name" : "math1 class", "startTime" : "2015-01-01T20:59:00.000Z", "endTime" : "2015-02-01T20:59:00.000Z", "fee" : 3001 } ] } ] } ], "teacher" : ObjectId("5552de1756c7e627ebceb1af") } 

但不知何故,这个方法没有任何回报。 其他types的数据,如数字(代码省略)的范围查询工作正常。 它是如何在Mongo控制台中工作的,而不是在服务器代码中。 任何见解都将非常有帮助。