考虑我定义了一个Mongoose 模式
var Schema_MySchema = new mongoose.Schema({
Field1: String, Field2: String
});
我通过以下方式添加了虚拟属性和设置选项:
Schema_MySchema.virtual('USERNAME').get(function () {
return this.Field1;
});
Schema_MySchema.set('toJSON', { virtuals: true });
使用 schema 对象从 MongoDB 中检索数据,如下所示:
var Mod_Obj = mongoose.model('SchemaName', Schema_MySchema);
var Model_Instance = new Mod_Obj();
Model_Instance.find({}, function (err, docs) {
/*
Here I get a object docs with following structure:
[{ _id: ObjectId("511XXXdff9c4c419000008"),
Field1: 'SOMEvalueFromMongoDB_1',
Field2: 'SOMEvalueFromMongoDB_2',
USERNAME: 'SOMEvalueFromMongoDB_1'
}]
*/
});
现在我想删除从 MongoDB 返回的实际属性,比如说我想从中Field1
删除docs
我尝试了以下方法来删除:
1. delete docs.Field1;
2. var Json_Obj = docs.toJSON(); delete Json_Obj.Field1;
3. var Json_Obj = docs.toObject({ virtuals: true }); delete Json_Obj.Field1;
4. delete docs[0].Field1;
5. delete docs[0]['Field1'];
所有的方法都没有奏效。:-(
如果在简单的 JSON 上执行测试,它可以工作:
var a = { 'A' : 1, 'B': 2 };
delete a.A;
console.log(a); //prints only object with B attribute only. i.e. { B: 2 }
谁能告诉我这里有什么问题?
提前致谢...