I have a document which has a array of times of type string. I want to retrieve all the values closest to given time and onward.
Hi Ali,
One way of achieving what you are after is to utilise MongoDB aggregation pipeline. You can use the $unwind operator to expand the document per Time
array element, and then filter the result using $match operator. For example:
// Retrieve all array elements greater than "17:30"
db.collection.aggregate([
{ $match: {"St_Name": "CitySquare" }},
{ $unwind: "$Time" },
{ $match: { "Time": { $gte : "17:30" }}}
]);
Depending on the complexity of the application and dataset, you may benefits from storing Time
values as date.
I would also recommend to enrol in a free online course at MongoDB University; especially the M101 courses that cover indexes, data modelling, querying, aggregation etc. The next session starts on the 24th May, there’s plenty of time to register.
Regards,
Wan.