我正在开发一个带有预先存在的MongoDB集合的MEAN Stack应用程序,如果我为.find()定义条件,则在没有条件的情况下没有返回任何结果。
以下是我的模型文件中的代码:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mainSchema = new Schema({
id: Number,
x: Number,
y: Number,
prio: Number,
type0: String,
type1: String,
type2: String,
width: Number,
height: Number,
text1: String,
text2: String,
size1: Number,
font: String,
color1: String,
color2: String,
links: String,
peers: String
}, { collection: 'main' });
mainSchema.statics = {
load: function(l, t, r, b, cb){
console.log(l, t, r, b);
return this.find({
x: { $gt: l, $lt: r },
y: { $gt: t, $lt: b }
}).exec(cb);
}
};
module.exports = mongoose.model('main', mainSchema);
这是无条件输出中的一个对象:
[
{
"_id": "577faf952a7c33f2fe44b282",
"id": 4,
"x": 50944,
"y": 54995,
"prio": 1,
"type0": "a",
"type1": "a",
"type2": "a",
"width": 100,
"height": 100,
"text1": "Chemie",
"text2": "",
"size1": 48,
"font": "f1_a ",
"color1": "#000000",
"color2": "#bfdeff",
"links": "14,53445,57328,12,#ff3d3d,k&13,54744,53904,12,#8c8c86,k&12,52557,51870,12,#f2ff12,k&11,51172,49743,12,#2312ff,k&10,48270,47335,12,#49fe6e,k&",
"peers": "1"
}
]
以下是调用加载方法的代码:
var mongoose = require('mongoose');
var main = require('../models/main');
exports.load = function(req, res){
main.load(parseInt(req.query.l), parseInt(req.query.t), parseInt(req.query.r), parseInt(req.query.b), function(err, data) {
res.jsonp(data);
});
};
答案 0 :(得分:0)
问题解决了! 尽管输出所有数据都存储为字符串而不是MongoDB中的int。 我只是将JSON输出(类型正确)复制到文件并将其导入MongoDB,现在代码工作正常。