我有一个看起来像这样的搜索方法。 (我还有很多其他代码用于进行不同类型的搜索,但为了简单起见我省略了这一点)
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;
}
我给出了像这样的搜索参数
{
"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") }
但不知怎的,这个方法什么都没有给我回报。其他类型的数据的范围查询,如数字(代码被省略)工作正常。如何在mongo控制台中使用相同的条件,而不是在服务器代码中。任何见解都会非常有用。