无法修改从Mongoose文档search对象
我想要一个通用的REST,返回给定模式的所有logging。
/* Read all entries for a given document type, TODO: limit this to a sensible amount of records, say 500 */ app.get( '/data/all/:id' , verifySession , function( req, res ) { exposed[req.params.id].find( {} , function(err,docs) { if( docs && req.params.id == "Account" ) docs.forEach( function(o){ console.log(o); delete o.salt; delete o.hash; console.log(o); } ); res.json( err || docs ); }); });
而对于帐户,我不想返回hash
和salt
,但行为就像它只是只读。 第二个console.log(o)仍然有salt
和hash
。
帮帮我?
Mongoose返回Document实例,它们不是普通的对象。
所以你需要首先使用toObject
来转换它们:
var documents = docs.map( function(doc) { doc = doc.toObject(); delete o.salt; delete o.hash; return doc; });
或者,您可以告诉find
中排除hash
和salt
字段:
exposed[req.params.id].find({}, '-hash -salt', function(err, docs) { ... });