【问题标题】:populate in mongodb with meteor用流星填充mongodb
【发布时间】:2015-12-18 02:51:53
【问题描述】:

我正在使用meteor.js。我有三个收藏板、类别、用户。

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 引用。这就是试图得到它的方式。

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

   })
 })

因为在流星中使用猫鼬会影响性能,所以我不能使用填充方法。那么,除了上述方法之外,还有其他方法可以实现结果吗?

【问题讨论】:

标签: mongodb meteor


【解决方案1】:

可能是collection helpers

基本用法:

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

在模板中的使用非常简单。假设你有你的董事会:

{{#each boards}}
  <div>
    <h3>{{board_name}}</h3>
    <p>Created by</p>: {{ creator.username }}
    <p>Category</p>: {{ category.catname }}
  </div>
{{/each}}

添加提示:使用publish-composite 以更易于管理的方式发布关系。

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

【讨论】:

  • 谢谢。但是我必须从我使用 ddp 服务连接的其他应用程序中调用它。如何连接它们?
  • 我解决了。但面临另一个问题。问了一个同样的问题。请帮我解决? stackoverflow.com/questions/32717394/…
猜你喜欢
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
  • 2021-01-25
  • 2019-06-25
  • 1970-01-01
相关资源
最近更新 更多