populate in mongodb with meteor -
i using meteor.js. having 3 collections boards,categories,users.
boards : { "_id": objectid("su873u498i0900909sd"), "locked": numberint(0), "board_name": "legends", "description": "legends of world", "cat_id": objectid("su873u498i0900909sd"), "cost": numberint(1), "image": "1389249002691_indexhj.jpeg", "creator": objectid("52ce317994acdcee0fadc90c") } categories: { "_id": objectid("su873u498i0900909sd"), "user_id": objectid("su873u498i0900909sd"), catname:"hjjj" } users: { "_id":objectid(55acec2687fe55f12ded5b4a), "username" :"mariya" }
from want username in users collection referred in categories collection user_id field categories collection referred in boards collection cat_id. how trying it.
boards.find({_id:"objectid(55acec2687fe55f12ded5b4a)"},function(res){ if(res){ categories.find({_id:res.cat_id},function(res1){ if(res1){ users.find({_id:res.user_id},function(res3){ res.send(res3) }) }) })
as using mongoose in meteor affect performance cannot use populate method. there other way achieve result rather above one?
probably collection helpers.
basic usage:
boards.helpers({ creator: function () { return meteor.users.findone(this.creatorid); }, category: function () { return categories.findone(this.categoryid); } });
usage in template pretty simple. let's have board:
{{#each boards}} <div> <h3>{{board_name}}</h3> <p>created by</p>: {{ creator.username }} <p>category</p>: {{ category.catname }} </div> {{/each}}
added tip: use publish-composite publish relationships in more manageable fashion.
meteor.publishcomposite('board', function (boardid) { check(boardid, string); return { find: function () { return boards.find(boardid); }, children: [{ find: function (board) { return meteor.users.find(board.creatorid); } }, { find: function (board) { return categories.find(board.categoryid); } }] } });
Comments
Post a Comment