Complex Mongo query with ors and ands

Hello,
I’m trying to perform a query based on many filters.
Right now it shows me all of the data the user is subscribed to, which isn’t what I need. I want it to look at field a and b, and check if there’s that filtered company in either of those, filter that by the year and sort.

This is what my query looks like:

return Trips.find({"year": {$eq: yearNum} , "userId" : {$eq: Meteor.userId()} }, { $or: [ { "a": { $eq: companySelected } },  { "b": { $eq: companySelected } }] },  {sort:{createdAt: -1}});

You’ve got misaligned braces (never good :wink:):

return Trips.find({ year: { $eq: yearNum }, userId: { $eq: Meteor.userId() }, { $or: [{ a: { $eq: companySelected } }, { b: { $eq: companySelected } }] } }, { sort: { createdAt: -1 } });

(I think).

EDIT: not 100% sure I understand what you want!

1 Like

Thank you very much. I wrote this and this does the trick:

  	return Trips.find({ $or: [ { "a": { $eq: companySelected } },  { "b": { $eq: companySelected } }], "year": {$eq: yearNum},  "userId" : {$eq: Meteor.userId()}}, {sort:{createdAt: -1}});

Thank you for an uber fast response tho! Best of luck to you!!

1 Like