node.js Meteor - collection.find() 总是返回所有字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15961456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:11:35  来源:igfitidea点击:

Meteor - collection.find() always returns all fields

node.jsmongodbmeteor

提问by redcap3000

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.

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

Commands like

命令像

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

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.

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

回答by nate-strauser

your syntax is off a bit, it should be

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

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

or

或者

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

您的模板确实控制显示哪些数据,但仍有许多场景限制字段有意义 - 数据隐私或效率(所有记录的某些字段,“当前”记录的所有字段)是两个常见的

you didnt mention it, but this usually is within a publish function - see http://docs.meteor.com/#meteor_publish- the fieldsmodifier 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

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

--

——

double check that you've removed the autopublishpackage 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

仔细检查您是否也删除了该autopublish软件包,但是如果您有该软件包并编写自己的发布函数,您应该会看到警告,这是您最常使用的地方fields

回答by L.T

First, if you want to control some fields in Collection.find(),you can try to do it this way:

首先,如果你想控制 中的某些字段Collection.find(),你可以尝试这样做:

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

but it was working only on the server.

但它只在服务器上工作。

Or try this:

或者试试这个:

On the server:

在服务器上:

Meteor.publish("myCollection", function () {
    return SvseTree.find({},{fields: {field:1}});
});

On the client:

在客户端:

Meteor.subscribe("myCollection");

then run meteor remove autopublish.

然后运行meteor remove autopublish

Second, if you want to get Array of Collection.find(), try to do it: Collection.find().fetch();

其次,如果你想得到Collection.find()的Array,尝试这样做:Collection.find().fetch();