【问题标题】:Mongoose does not return properties that are empty objectsMongoose 不返回空对象的属性
【发布时间】:2021-08-15 20:34:32
【问题描述】:

当我从 MongoDB 获取记录时,获取的文档没有数据库中存在的属性,但它们是空对象。我应该添加一些配置来获取空对象的属性吗?

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    您需要将minimize 设置为false。

    官方示例:

    const schema = new Schema({ name: String, inventory: {} }, { minimize: false });
    const Character = mongoose.model('Character', schema);
    
    // will store `inventory` if empty
    const sam = new Character({ name: 'Sam', inventory: {} });
    await sam.save();
    doc = await Character.findOne({ name: 'Sam' }).lean();
    doc.inventory; // {}
    

    【讨论】:

      猜你喜欢
      • 2018-05-18
      • 2018-08-07
      • 2020-09-14
      • 2018-04-03
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 2022-01-23
      • 2019-03-09
      相关资源
      最近更新 更多