How to filter documents in mongodb based on an interval?

85 Views Asked by At

I have 2 fields on my document:

class MyDoc {
   int to;
   int from;
}

And I have an array with values as input: [4, 5, 6]

I want to find all documents whereby all the elements in the given array fit between that to and from interval.

For a single value it would be easy since I can use lt and gt but how to do it for all the array elements?

Much appreciated.

1

There are 1 best solutions below

0
On

You can try it using this:

// givenArray = [4,5,6]
.find({
    to : { $in: givenArray},
    from: { $in: givenArray}
})