Meteor find distinct

Hi,

I have read several articles about distinct queries and can’t put it to work for me. I have read underscore but in the end the only thing i get is: “Exception from sub “foo” id mgqThnxXtjSdwGP8T Error: Publish function returned an array of non-Cursors”

I want to get a full list of my data, with distinct field (no repeating), using meteor publish (if possible).

Thanks,
sfm

What about https://atmospherejs.com/monbro/mongodb-mapreduce-aggregation

Show your code which is causing the error then it’ll be easier to debug

Meteor.publish("pesquisaEquipamentos", function(search) {

  var data = Equipamentos.find().fetch();
  var distinctData = _.uniq(data, false, function(document) {
    return document.npatrimonio;
  });
  return _.pluck(distinctData, "npatrimonio");
});

I get this error:

Exception from sub “foo” id euGMAMcMparSKGPLf Error: Publish function returned an array of non-Cursors

Your code is returning an array of npatrimonio fields instead of returning distinctData.

And it would be much better if you fetched only the needed field instead of the entire database.

What you are doing with the above code is returning an array of npatrimonio. This is what you are returning: [1,2,3]. But it should be more like [{npatrimonio:1}, {npatrimonio:2}, {npatrimonio:3}].

For distinct queries you should use a method and the rawcollection. _.uniq is inefficient, take a look at this thread:

[quote=“nlammertyn, post:7, topic:17443”]
For distinct queries you should use a method and the rawcollection. _.uniq is inefficient
[/quote]Wouldn’t a method lose Optimistic UI because rawcollection is server only? Seems like there are arguments for both approaches.

Hi,
I put the result in a Session and it works.
Thank you all for the contribute

That’s absolutely true. It’ll probably depend on the use case, in general if I needed to run a distinct query it was on datasets that were large enough to compromise on the reactivity.

The problem with Sessions is that i´m using to mutch, Sessions for every thing :slight_smile: