[SERVER-31317] createView is interpreting Date object at creation time Created: 28/Sep/17  Updated: 07/Nov/17  Resolved: 29/Sep/17

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 3.4.9
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Michael Felix Dias Assignee: Kelsey Schubert
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-23656 Current timestamp variable for aggreg... Closed
Operating System: ALL
Participants:

 Description   

To get the last documents based on their datetime property we can use:

db.lightningsStrikes.find({ datetime: { $gte: new Date(new Date() - 1000*60*20) } })

The command above gets lightning strikes of last twenty minutes.
To create a view of it we should to be able to do this: right?

db.createView("lastLightningStrikes", "lightningStrikes", [{
    $match: {
        datetime: {
            $gte: new Date(new Date() - 1000*60*20)
        }
    }
}])

But createView is interpreting new Date() at creation time instead of querying time, as below:

db.system.views.find().pretty()

{
	"_id" : "lightning.latestLightningStrikes",
	"viewOn" : "lightningStrikes",
	"pipeline" : [
		{
			"$match" : {
				"datetime" : {
					"$gte" : ISODate("2017-09-28T19:25:34.410Z")
				}
			}
		}
	]
}

I suppose that it should be as below:

{
	"_id" : "lightning.latestLightningStrikes",
	"viewOn" : "lightningStrikes",
	"pipeline" : [
		{
			"$match" : {
				"datetime" : {
					"$gte" : new Date(new Date() - 1000*60*20)
				}
			}
		}
	]
}



 Comments   
Comment by Kelsey Schubert [ 29/Sep/17 ]

Unfortunately, there is currently no way to generate an expression inside aggregation which evaluates to current date/time. As mentioned, SERVER-23656 would provide this solution.

Comment by Michael Felix Dias [ 29/Sep/17 ]

Ok, cool! but is there an alternative way to reach the same result today?

Comment by Kelsey Schubert [ 29/Sep/17 ]

Hi michaelfdias,

Thank you for opening this ticket. The behavior you describe is expected, however we have an open feature request that would provide the functionality I believe you're looking for. Please feel free to review, vote, and watch SERVER-23656 for updates.

Kind regards,
Kelsey

Generated at Thu Feb 08 04:26:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.