Limit number of field return from mongodb in Meteor

不问归期 提交于 2019-12-25 04:25:25

问题


I am have a collection that stores documents that contains user informations like Email, Phone Number, etc.

I want to be able to return only 5 fields that exist in the document, for example.

Email: [a@a.com, b@b.com]

Mobile: [012345, 6789]

Country: USA

Service: [Wholesale, Retail]

Zip: 30001

City: New York

It will return:

a@a.com, 012345, USA, Wholesale, 30001

or if the document is

Service: [Wholesale, Retail]

Zip: 30001

City: New York

It will return:

Wholesale, 30001, New York

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.

Any help is highly appreciated.

Edit: It would be fine even if I could impose this kind of limit on handlebars/spacebars instead of cursor. Whatever works~


回答1:


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 ;
};



回答2:


Use Field Specifiers to choose what you want.



来源:https://stackoverflow.com/questions/21086466/limit-number-of-field-return-from-mongodb-in-meteor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!