Mongoose find返回一个空白数组,RoboMongo查找数据

时间:2018-01-24 22:21:12

标签: mongodb mongoose mongoose-schema

我有一个Express应用程序,直到今天早上才从数据库返回一个对象数组。现在它返回一个空数组。 RoboMongo告诉我数据仍然存在并且运行良好。有什么想法吗?

我的模特:

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,
        changeInPcnt: Number,
        currentRipeness: String,
        nextStage: String,
        timeToNextInDays: Number,
        status: Number
    },
    { bufferCommands: false },
    { collection: 'plotStatuses' }
);

const ModelClass = mongoose.model(
    'plotStatus',
    plotStatusSchema,
    'plotStatuses'
);

module.exports = ModelClass;

我的回归控制员:

const PlotStatus = require('../models/plotStatus');
const jsonpack = require('jsonpack');

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);
    }
};

我的数据样本:

{
    "_id" : ObjectId,
    "recordDate" : ISODate,
    "blockName" : String,
    "blockCode" : String,
    "growerName" : String,
    "company" : String,
    "variety" : String,
    "planted" : ISODate,
    "region" : String,
    "yieldInKG" : Number,
    "changeInPcnt" : Number,
    "currentRipeness" : String,
    "nextStage" :String,
    "timeToNextInDays" : Number,
    "status" : Number,
    "targetYieldInKG" : Number,
    "currentStatePercentage" : Number,
    "totalLengthOfPhase" : Number,
    "nextPhaseStart" : ISODate,
    "currentBrix" : Number,
    "currentPh" : Number,
    "currentTA" : Number,
    "plotGeoJSON" : Object,
    "historicalData" : Array
}

我知道Schema不再匹配JSON的形状,但是我可以返回所有符合查找条件的JSON吗?

1 个答案:

答案 0 :(得分:1)

您正在搜索字符串" req.user.companyCode"在company属性中。显然,您的数据中没有公司代码。再试一次没有问题:

const plotStatus = await PlotStatus.find({
    company: req.user.companyCode
}).lean();