问题
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