17

I have the following schema:

var sampleSchema = new Schema({
  name: String,
  dates: [{
    date: Date,
    duration: Number
  }]
});

I'd need to filters the records according to the following rule: if one of dates is later than a given date date_begin, keep the record, otherwise, don't.

I have the impression that $gte or $lte are the function I need, but I can't find a way to use them correctly. I tried

sampleSchema.find({date_begin: {$gte: 'date'}});

or some variants of that, but I can't seem to be able to make it work. Anyone has an idea of how I am supposed to do this?

1 Answer 1

20

To do querying on elements inside arrays, $elemMatch is used :

SampleModel.find( { dates : { $elemMatch: {  date : { $gte: 'DATE_VALUE' } } } } )

If you're using a single query condition, you can directly filter:

SampleModel.find( { 'dates.date': { $gte: 'DATE_VALUE' } } )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.