yichen
yichen

Reputation: 511

Mongoose $or query condition with $and condition

I am trying to get this simple query to work with Mongoose with no luck: find all events happening tomorrow at the conference center. For each event, the schedule can be either a specific begin time as a Date type, or an array of dates in string format '2012/11/16'. The same query.or condition works however when there is no condition for {place:'conference center'}, so I thought I might be composing this query wrong. I have no luck so far, suggestions/help please? The "tomorrow" condition is a filter added onto the "place" condition, so I am using the query chaining.

Thanks!

    Event.find({place:'conference center'}).or([
      { 
        'schedule.beginAtUtc' : { $gte: startOfTomorrow },
        'schedule.beginAtUtc' : { $lt: endOfTomorrow }
      },

      {
        'schedule.dates' : '2012/11/17'
      }
    ]);

Upvotes: 2

Views: 7137

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

You have to combine the two 'schedule.beginAtUtc' fields into one as an object can't have two fields with the same name.

Like this:

Event.find({place:'conference center'}).or([
  { 
    'schedule.beginAtUtc' : { $gte: startOfTomorrow, $lt: endOfTomorrow }
  },

  {
    'schedule.dates' : '2012/11/17'
  }
]);

Upvotes: 4

Related Questions