corvid
corvid

Reputation: 11187

create a new field and project data on the fly with Meteor

Let's say I have a mongo document that has the following format.

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

So, for simplicity's sake, let's say I want to take the one object in profile which is not null and project it into a new field called "profile" to use on my client side.

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

I understand that this could likely be done with aggregation, however, I cannot find anything for meteor that works on both the client and the server for aggregation.

Although I know this is fairly easy to do with underscore, I feel like it will add layers of complexity by taking it away from mongo. Mongo solutions would be preferred.

Using meteor, is it possible to project a field into a new field for publication?

Upvotes: 0

Views: 131

Answers (1)

chridam
chridam

Reputation: 103365

Using the example in your question, you could use the transform option which allows behaviour to be attached to the objects returned via the pub/sub communication to project the new field. Something like this:

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

You can also check out this nice meteor-collection-hooks package.

Upvotes: 1

Related Questions