【问题标题】:Mongoose findById is not returning all fieldsMongoose findById 没有返回所有字段
【发布时间】:2021-02-05 22:34:18
【问题描述】:

我正在使用 mongoose 调用 findById,但它没有返回所有字段,或者至少它没有正确映射到某个字段。但如果我使用聚合,它会返回该字段

我有以下架构

const ratingSchema = new mongoose.Schema({
    rating: {
        type: Number,
        default: 0,
        min: 0,
        max: 5
    }
})

const locationSchema = new mongoose.Schema({ 
    name: {
        type: String,
        required: true,
    },
    address: {
        type: String,
        required: true,
    },
    rating: ratingSchema,
    
    facilities: [String],
});

locationSchema.index({coords: '2dsphere'});

mongoose.model('Location', locationSchema);

当我打电话时

const Loc = mongoose.model('Location');

const locationsReadOne = (req, res) => {
    Loc
        .findById(req.params.locationid)
        .exec((err, location) => {
            if (!location) {
                return res.status(404).json({"message":"location not found"});
            } else if (err) {
                return res.status(404).json(err);
            }
            console.log("locationsReadOne:",location);
            res.status(200).json(location);
        });
};

它返回架构,但未返回评级字段。这是返回对象的 console.log:

locationsReadOne: {
  facilities: [ 'Hot drinks', 'Food', 'Premium wifi' ],
  _id: 5f88bfdc4df4ca7709462865,
  name: 'Starcups',
  address: '125 High Street, Reading, RG6 1PS'
}

如果我调用 Loc.aggregate,则返回评级字段:

{
  _id: 5f8b2ee15b0b6784a847b600,
  facilities: [ 'Tea', ' Restroom' ],
  name: 'Tea Leaf',
  address: '2192 Green St.',
  rating: 0,

}
{
  _id: 5f88bfdc4df4ca7709462865,
  name: 'Starcups',
  address: '125 High Street, Reading, RG6 1PS',
  rating: 3,
  facilities: [ 'Hot drinks', 'Food', 'Premium wifi' ]
}

知道为什么会发生这种情况吗?我可以清楚地看到 MongoDB 罗盘中每个文档中的评级字段,它们都被列为 Double 类型。为什么它们被汇总返回,而不是在 findById(id) 甚至在 find() 中?

谢谢。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    当您在 mongoose 中使用 find 时,它将使用架构中的字段填充文档。例如 - 如果您从架构中删除一个字段,您可能在找到的文档上看不到该字段,即使它在数据库中也是如此。 Aggregate 和 Compass 准确地向您显示数据库中的内容,因此数据就在那里,只是没有在文档中填充,因为它在架构上看不到它。

    原因是您的ratingSchema 是一个具有rating 属性的对象。所以猫鼬正在寻找类似的东西:

    {
      name: ...
      rating: {
         rating: 2 
      }
    }
    

    并没有找到它,所以它没有填充。为了解决这个问题,我不会将 rating 定义为子模式,而是将您的模式定义为

    const locationSchema = new mongoose.Schema({ 
        name: {
            type: String,
            required: true,
        },
        address: {
            type: String,
            required: true,
        },
        rating: {
            type: Number,
            default: 0,
            min: 0,
            max: 5
        },   
        facilities: [String],
    });
    

    【讨论】:

    • 如果我需要/想要使用子模式怎么办?在这种情况下,评级只在一个模式中使用,所以它真的不需要分开。但是,如果我想在多个地方使用它怎么办?
    • 子模式通常用于嵌套对象。如果你只是想分享一个属性的验证,你可以使用普通的旧 JS。 const ratingType = { type: Number ...} 然后在您的架构上:{ rating: ratingType }
    • 这有效并保持了验证!再次感谢您的帮助!!
    猜你喜欢
    • 2016-06-24
    • 2018-06-17
    • 2019-07-15
    • 1970-01-01
    • 2018-12-10
    • 2018-05-29
    • 2014-04-04
    • 2018-04-19
    • 2018-10-04
    相关资源
    最近更新 更多