我正在使用Express在Mongo集合中执行查找操作并返回结果。我知道集合中有数据,但结果数组返回空。我怀疑这是架构的一个问题,但无法弄清楚是什么。想法?
路线:
const PlotStatus = require('../ models / plotStatus');
exports.plotStatuses = async (req, res) => {
const plotStatus = await PlotStatus.find({
company: req.user.companyCode
}).lean();
if (!plotStatus) {
throw new Error('Plot Statuses not found');
} else {
res.send(plotStatus);
}
};
型号:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const plotStatusSchema = new Schema(
{
recordDate: Date,
blockName: String,
growerName: String,
company: String,
variety: String,
planted: Number,
region: String,
yieldInKG: Number,
currentRipeness: String,
nextStage: String,
timeToNextInDays: Number,
status: Number
},
{ bufferCommands: false },
{ collection: 'plotStatuses' }
);
const ModelClass = mongoose.model('plotStatus', plotStatusSchema);
module.exports = ModelClass;
示例plotStatus:
{
"_id" : ObjectId("stringhere"),
"recordDate" : ISODate("2018-01-02T12:50:51.236Z"),
"blockName" : "name",
"growerName" : "grower's name",
"company" : "mycompany",
"variety" : "myvariety",
"planted" : 2010,
"region" : "myregion",
"yieldInKG" : 960,
"changeInPcnt" : -1.6,
"currentRipeness" : "ripeness",
"nextStage" : "nextstage",
"timeToNextInDays" : 42,
"status" : 0
}
答案 0 :(得分:0)
好的,明白了。
通常,Mongoose会从模型名称中推断出集合名称,但您可以显式传递它。显然,plotStatus => plotStatuses太过分了。这修好了它:
const ModelClass = mongoose.model(
'plotStatus',
plotStatusSchema,
'plotStatuses'
);