Meteor - collection.find() 总是返回所有字段 [英] Meteor - collection.find() always returns all fields

查看:20
本文介绍了Meteor - collection.find() 总是返回所有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到这个(有点烦人的问题)我试图查找集合中的所有记录,而不显示(或显示)特定字段(分数).这只是一个例子,并不反映我的实际代码,但问题总是可重现的.我排除的字段非常大,我只是想生成一个可用记录菜单.

Ran into this (slightly annoying problem) I'm trying to look up all records in a collection and not show (or show) a specific field (score). This is just an example and not reflective of my actual code, but the problem is always reproducible. The fields I am excluding are very large and I'm merely trying to generate a menu of available records.

    players.find({},{score:1})
    players.find({},{score:0})

总是返回每个字段,而不是在 mongodb 中触发 exclude/include.我什么都不担心,因为模板可能可以控制将哪些数据呈现为 html?仍然感觉数据传输到客户端无论如何;并显示在控制台中.

Always return every field, instead of triggering the exclude/include in mongodb. Am I worried about nothing, since the template potentially can control what data gets rendered to html? Still feels like the data is transferred to the client side regardless; and shows up in the console.

推荐答案

你的语法有点不对,应该是

your syntax is off a bit, it should be

CollectionName.find({}, {fields: {'onlyThisField':1}});

CollectionName.find({}, {fields: {'everythingButThisField':0}});

你的模板确实控制了显示什么数据,但仍然有很多场景限制是有意义的——数据隐私或效率(所有记录的某些字段,当前"记录的所有字段)是两个常见的

your template does indeed control what data is displayed, but there are still many scenarios where field limiting makes sense - privacy of data or efficiency (some fields of all records, all fields of the 'current' record) are two common ones

你没有提到它,但这通常是在发布函数中 - 见 http://docs.meteor.com/#meteor_publish - fields 修饰符也可以在客户端使用,但它不限制发送到客户端的数据,只是发送到模板 - 服务器端字段减少/选择有不同的好处

you didnt mention it, but this usually is within a publish function - see http://docs.meteor.com/#meteor_publish - the fields modifier is also available on the client, but there it does not limit data sent down to client, just to the template - server side field reduction/selection has different benefits

--

仔细检查您是否也删除了 autopublish 包,但是,如果您激活了该包并编写自己的发布函数,您应该会看到警告,这是您最常使用的 字段

double check that you've removed the autopublish package too, however you should see a warning if you have that active and write your own publish functions, which is where you would most commonly use fields

这篇关于Meteor - collection.find() 总是返回所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆