How to publish a view/transform of a collection in Meteor?

前端 未结 3 485
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 02:39

I have made a collection

var Words = new Meteor.Collection(\"words\");

and published it:

Meteor.publish(\"words\", functio         


        
3条回答
  •  春和景丽
    2020-12-03 02:49

    You could assemble the counts by going through each document in Words, (cursor for each)

    var countingCursor = Words.find({});
    var wordCounts = {};
    
    countingCursor.forEach(function (word) {
      wordCounts[word.length].count += 1;
      wordCounts[word.length].words = wordCounts[word.length].words || []
      wordCounts[word.length].words.push(word);
    });
    

    create a local collection,

    var counts = new Meteor.Collection('local-counts-collection', {connection: null});
    

    and insert your answers

    var key, value;
    
    for (key in wordCounts) {
      value = object[key];
      counts.insert({
        length: key,
        count: value.count,
        members: value.words
      });
    }
    

    Counts is now a collection, just not stored in Mongo.

    Not tested!

提交回复
热议问题