Passing selectors from client for inexpensive DB querying
I'm trying to specify selectors for the meteor collection.find in order to implement search functionality in my app. I know 'how to' questions are best for SO but I got nowhere there.
1st attempt:
// inside on of the controllers (Client)
var selectors = {"make.name":"Toyota"}
var test = Listings.find(selectors);
var test2 = Listings.findOne(selectors);
console.log(test); // undefined for obvious reasons
console.log(test2);// logs only one listing
2nd attempt:
Listing = new Mongo.Collection('listings');
// Server
Meteor.publish('listings', function(selectors) {
if (selectors) {
return Listings.find(selectors);
}
return Listings.find();
});
// Client
var test = $meteor.collection(Listings);
var selectors = {"make.name":"Toyota"}
$meteor.subscribe("listings", selectors);
console.log(test); // logs everything in db instead of just listings with property "make.name" = "Toyota"
I just want to be able to pass the damn selectors to the server. :sob:
I think you flipped your logic. The server publish code should be if (selectors), or if (!!selectors).
Thank you so much for the response! if (selectors) is actually what I have, it was a typo I made posting the issue. I'm still getting the entire db, could it be that I'm timing the $meteor.subscribe call incorrectly? or passing the selecters var in the wrong place? I'm able to get around this issue if I try something like Meteor.subscribe('listings', selectors) but this feels hacky, and it breaks all other parts of my app that use the 'listings' collection.
I would suspect that there is another subscription which get the full listing, and in this case the collection gets everything. try and remove the return Listings.find(); If you want to further discuss it - I suggest to go on gitter.
That ^ + $or syntax did the trick; thanks!
Btw, be sure to check your selectors on the server, to avoid stuff like: http://blog.east5th.co/2015/08/10/dos-your-meteor-application-with-where/
thanks everyone!