Guilherme Decampo
Guilherme Decampo

Reputation: 13

Who to use $or and $in at the same query in Meteor

Hi guys I'm facing some problems to use both $or and $in in Meteor.

I follow the book MongoDB The Definitive Guide:

db.raffle.find({$or : [{ticket_no : {$in : [725, 542, 390]}}, {winner : true}]})

But is not working. Maybe because the miniMongo doesn't accept ?

This is my code:

keyArray = Session.get("search_keywords").split(" ");
console.log(keyArray);
keywords = [];
for (var i = 0; i < keyArray.length; i++) {
  keywords.push(new RegExp(keyArray[i],"i"));
  console.log(keywords);
}
  console.log(keywords);
  data = Data.find({$or:[{title:{$in: keywords}}, {sort: {SortCreated: -1}}).fetch();
  console.log(sedis)

Upvotes: 0

Views: 79

Answers (1)

Matt C
Matt C

Reputation: 903

Your first query works fine for me via the meteor mongo command line.

However your second query doesn't work because you're passing a list to the $or operator, but there's only one thing in the list (and as @Alan Spencer said, the list isn't completed). Also, you can treat sort as a function. Try:

Data.find({title:{$in: keywords}}).sort({SortCreated: -1})

Add the .fetch() if you want to log the data, leave it out if you're passing to a template.

Upvotes: 1

Related Questions