在mongodb中填充流星 [英] populate in mongodb with meteor

查看:59
本文介绍了在mongodb中填充流星的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用meteor.js.我有三个收集板,类别,用户.

I am using meteor.js. I am having three collections boards,categories,users.

Boards : 
 {
 "_id": ObjectId("su873u498i0900909sd"),
  "locked": NumberInt(0),
 "board_name": "Legends",
  "description": "legends of the 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"
}

由此我想在用户集合中获取用户名,该用户集合在具有user_id的类别集合中引用,其中类别集合在具有cat_id的板集合中引用.这是尝试获取它的方式.

from this i want to get username in the users collection which is referred in categories collection with user_id field where the categories collection is referred in boards collection with cat_id. This is how an trying to get 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 will affect the performance i cannot use populate method. So is there any other way to achieve the result rather than above one?

推荐答案

可能收集助手.

基本用法:

Boards.helpers({
  creator: function () {
    return Meteor.users.findOne(this.creatorId);
  },
  category: function () {
    return Categories.findOne(this.categoryId);
  }
});

模板中的用法非常简单.假设您有董事会:

Usage in template is pretty simple. Let's say you have your 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 to publish the relationships in a 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);
      }
    }]
  }
});

这篇关于在mongodb中填充流星的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆