I have a demand looks a little complex:
I have a collection in which the document has a array called startTimeArray stored the timeStamps of a event. Such as:
"startTimeArr" : [
NumberLong("1425340800000"),
NumberLong("1425513600000"),
NumberLong("1426032000000"),
NumberLong("1425427200000")
]
I want the find result to be sorted by the minimum Time stamp that greater than the current time(ie, time has not passed yet, the minimum on ).for example, if current time stamp in millisecond is 1425513500000 then the time stamp participate in the sorting action of this document is 1425513600000, which is the minimum stamp of the startTimeArr array that are greater than 1425513500000.
So The element of startTimeArr participate in the sorting action should be the one:
- greater than the current time.
- the minimum one that meet with #1
I guess I can do this with a aggregate such as:
db.event.aggregate(
{
$match:{
startTimeArr:{
$gt: 1425513500000 // presume the current time is 1425513500000
}
}
},
{
$group:{
_id:"$_id", minTimeStamp:{?????} // and I don't know what to write here.
}
},
{
$sort:{minTimeStamp:1}
}
)
But I cant get a clue of how to write the minTimeStamp calculation.
Could you tell how to do this? Or if there are other ways to do this, will be appreciated too.
Since the problem is a bit too complex, I will just put some samples and outputs expected as required.
If we have 3 document like this:
{
"_id" : ObjectId("537d98c20cf2264603faf0eb"),
"name": "Let's go to LA",
"fee": 500,
"duration":2,
"startTimeArr":[
NumberLong("1425310000000"),
NumberLong("1425320000000"),
NumberLong("1425330000000"),
NumberLong("1425380000000")
]
}
{
"_id" : ObjectId("537caa7e0cf2264603faf0e9"),
"name": "Let's go to NewYork",
"fee": 800,
"duration":3,
"startTimeArr":[
NumberLong("1425320000000"),
NumberLong("1425330000000"),
NumberLong("1425340000000"),
NumberLong("1425350000000")
]
}
{
"_id" : ObjectId("537c5ec50cf2264603faf0e7"),
"name": "Let's go to Washington",
"fee": 700,
"duration":2,
"startTimeArr":[
NumberLong("1425350000000"),
NumberLong("1425360000000"),
NumberLong("1425370000000"),
NumberLong("1425390000000")
]
}
And if current time is 1425300000000 then the time participate the sorting will be:
LA 1425310000000
NewYork 1425320000000
Washington 1425350000000
So the find result of these three documents will be sorted as the order LA, NewYork, Washington
But when time moves on and the current time is 142534000000 then the time participate the sorting procedure will be:
LA 1425380000000
NewYork 1425350000000
Washington 1425350000000
So the find results expected to be sorted as the order NewYork, Washington, LA.
startTimeArris already sorted.