I want to build a query for a very dynamic collection.
An example:
I have a collection like
{
_id: ObjectId(),
value: x
// some other data
}
The example dataset has the values
{
value: 1
},
{
value: 1
},
{
value: 2
},
{
value: 3
},
{
value: 3
}
As you can see the same value can be there multiple times.
But if I run the following query it only returns the first with value: 3
db.collection.aggregate([
{
$sort: "$value"
},
{
$limit: 4
}
])
But what I want is at least 4 documents which include all occurrences of the values in them. So I want all where value: 3.
Edit
Sorry, the question might be a bit misleading. I want to have a complete result. So all with value: 3. It is for a public transport database and the value is the departure time. So I want at least the next 30 departures, but if 30 and 31 depart at the same time, I want the 31 also.