简体   繁体   English

将minimongo集合中的所有项目相互比较

[英]Compare all items in minimongo collection to each other

I created a local minimongo collection & I want to compare each item in the collection to all the other items (combination, not permutation). 我创建了一个本地的minimongo集合,我想将集合中的每个项目与所有其他项目(组合,而不是排列)进行比较。 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? 这可能与minimongo? My first thought was to use hasNext() and next() but those don't exist. 我的第一个想法是使用hasNext()next()但那些不存在。 Then I thought I could aggregate and group on unique combinations, but that doesn't exist on the client either. 然后我想我可以aggregate和分组独特的组合,但客户端上也不存在。

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. 有一个Cursor.forEach()方法来迭代你的集合,可以导致正确的方向(见这里: http//docs.meteor.com/#/full/foreach ),但在你的情况下,你需要更多的东西。 Maybe this will help: 也许这会有所帮助:

This is how forEach works: 这是forEach的工作原理:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM