限制流星中从mongodb返回的字段数 [英] Limit number of field return from mongodb in Meteor
问题描述
我有一个存储文档的集合,其中包含电子邮件、电话号码等用户信息.
I am have a collection that stores documents that contains user informations like Email, Phone Number, etc.
例如,我希望能够仅返回文档中存在的 5 个字段.
I want to be able to return only 5 fields that exist in the document, for example.
电子邮件:[a@a.com, b@b.com]
Email: [a@a.com, b@b.com]
手机:[012345, 6789]
Mobile: [012345, 6789]
国家:美国
服务:[批发、零售]
邮编:30001
城市:纽约
它会返回:
a@a.com,012345,美国,批发,30001
a@a.com, 012345, USA, Wholesale, 30001
或者如果文档是
服务:[批发、零售]
邮编:30001
城市:纽约
它会返回:
批发,30001,纽约
Wholesale, 30001, New York
基本上限制返回的字段数为5个字段,如果该字段是数组,则只返回该数组中的第一项.
Basically limits the number of fields to return to 5 fields, and if that field is an array, then only return first item in that array.
我的英语不是很好,但我已尽力解释我的问题,如果有任何部分不清楚,我很乐意尝试进一步解释.
My english is not that good but I have tried my best to explain my problem, if any part is unclear I'd be happy to try to explain further.
非常感谢任何帮助.
即使我可以对把手/空格键而不是光标施加这种限制,也没关系.什么都行~
It would be fine even if I could impose this kind of limit on handlebars/spacebars instead of cursor. Whatever works~
推荐答案
这个想法是将你的收藏转移到另一个(结果)数组中,并在你的把手模板中检索这个数组:
The idea is to transfer your collection in another ( result ) array, and to retrieve this array in your handlebar template :
Users = new Meteor.Collection("users");
LIMIT =4;
Template.userShow.users = function () {
var users = Users.find().fetch();
for (var i = 0; i < LIMIT; i++) {
result[i] = users[i];
}
return result ;
};
这篇关于限制流星中从mongodb返回的字段数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!