有没有办法对集合上的所有查询基本上强制执行投影规则?因此,例如,如果我有一个集合widget,有没有办法确保secretAttribute默认情况下不会在查询中返回文档中存在的字段,除非查询projection明确要求它。与自动投影方式相反,_id除非投影参数明确拒绝它。例如,我可能有一个widget文件:
{ _id: '51a4e3962dfff00105000009', name: 'foo', color: 'white', status: 'open', secretAttribute: 'bar' }
如果我这样做db.widget.find({color: 'white'}),它将返回文档但不secretAttribute可见:
{ _id: '51a4e3962dfff00105000009', name: 'foo', color: 'white', status: 'open' }
我必须secretAttribute在projection参数中明确要求find():
db.widget.find({color: 'white'}, {name:1, color:1, status: 1, secretAttribute: 1})为拿到它,为实现它。
谢谢