使用Meteor动态创建新的字段和项目数据

时间:2015-06-03 13:09:40

标签: mongodb meteor

假设我有一个mongo文档,其格式如下。

{
  _id: "someid",
  profiles: {
    admin: {
      metadata: {
        addedAt: ISODate(),
        addedBy: 'someidornull'
      },
      recent: {
        transactions: []
      }
    },
    player: null,
    anonymous: null
  }
}

因此,为简单起见,假设我想在配置文件中使用非空的一个对象,并将其投影到一个名为“profile”的新字段中,以便在我的客户端使用。

{
  _id: "someid",
  profile: {
    metadata: {
      addedAt: ISODate(),
      addedBy: 'someidornull'
    },
    recent: {
      transactions: []
    }
  }
}

我知道这可能会在aggregation完成,但我找不到works on both the client and the server for aggregation的流星。

虽然我知道使用下划线很容易做到这一点,但我觉得它会通过将它从mongo中删除来增加复杂性。 Mongo解决方案将是首选。

使用meteor,是否可以将字段投影到新字段中进行发布?

1 个答案:

答案 0 :(得分:1)

使用问题中的示例,您可以使用transform选项,该选项允许将行为附加到通过pub / sub通信返回的对象以投影新字段。像这样:

Items.find({ /* selector */ }, {
    transform: function(item){
        item.profile = item.profiles.admin;
        return item;
    }
}); 

您还可以查看这个不错的 meteor-collection-hooks 包。

相关问题