0

I created a local minimongo collection & I want to compare each item in the collection to all the other items (combination, not permutation). If it were an array, it'd look like this:

for (var i = 0; i < coordCount - 1; i++) {
  for (var j = i + 1; j < coordCount; j++) {
    console.log(i,j);
  }
}

Is this possible with minimongo? My first thought was to use hasNext() and next() but those don't exist. Then I thought I could aggregate and group on unique combinations, but that doesn't exist on the client either.

Matt K
  • 4,813
  • 4
  • 22
  • 35

1 Answers1

0

There is a Cursor.forEach() method to iterate through your collections that could lead in the right direction (see here: http://docs.meteor.com/#/full/foreach), but in your case you need something more. Maybe this will help:

This is how forEach works:

// sort your cursor to get always reproducable results
var items = Items.find({}, {sort: {someprop: 1}});
var count = Items.count();
items.forEach(function (item, idx, cursor) {
  // item is your document
  // idx is a 0 based index
  // cursor is the.. cursor
  ...
  // break if idx >= count - 1
});

This could be a solution (though not very elegant and potentially memory hungry)

// sort your cursor to get always reproducable results
var items = Items.find({}, {sort: {someprop: 1}}).fetch();

_.each(items, function (item, idx) {
  // item is your document
  // idx is a 0 based index
  if (idx >= items.length - 1) {
    return;
  }

  yourCompareMethod(item, items[idx + 1]);
});
SCYV
  • 11
  • 4