在具有设置为toObject()
的(嵌套)属性的文档上调用null
时,属性将被删除,但仅在向getters
选项提供true
时},见下面的例子。
var mongoose = require('mongoose');
const schema = new mongoose.Schema({
customer: {
name: { type: String, required: false },
},
});
var Model = mongoose.model('Model', schema);
var model = new Model();
model.customer = null;
console.log(model.toObject());
console.log(model.toObject({ getters: true }));
//Prints:
//{ _id: 58e00dcd71bf5916802bdb3c, customer: null }
//{ _id: 58e00dcd71bf5916802bdb3c, id: '58e00dcd71bf5916802bdb3c' }
这是为什么? 我使用的是mongoose v.4.9.2