diff --git a/lib/server/query.js b/lib/server/query.js index 0b5de5601b9..cdbf51dacd2 100644 --- a/lib/server/query.js +++ b/lib/server/query.js @@ -94,7 +94,11 @@ function create (params, opts) { var query = finder && finder.find ? finder.find : { }; // Ensure some kind of sane date constraint tied to an index is expressed in the query. - enforceDateFilter(query, opts); + // unless an ID is provided, in which case assume the user knows what they are doing. + if (! query._id ) { + enforceDateFilter(query, opts); + } + // Help queries for _id. updateIdQuery(query); diff --git a/tests/query.test.js b/tests/query.test.js new file mode 100644 index 00000000000..92a8a80673a --- /dev/null +++ b/tests/query.test.js @@ -0,0 +1,38 @@ +'use strict'; + +require('should'); + +var moment = require('moment'); + +describe('query', function ( ) { + var query = require('../lib/server/query'); + + it('should provide default options', function ( ) { + var opts = query(); + + var low = moment().utc().subtract(4, 'days').subtract(1, 'minutes').format(); + var high = moment().utc().subtract(4, 'days').add(1, 'minutes').format(); + + opts.date['$gte'].should.be.greaterThan(low); + opts.date['$gte'].should.be.lessThan(high); + }); + + it('should not override non default options', function ( ) { + var opts = query({}, { + deltaAgo: 2 * 24 * 60 * 60000, + dateField: 'created_at' + }); + + var low = moment().utc().subtract(2, 'days').subtract(1, 'minutes').format(); + var high = moment().utc().subtract(2, 'days').add(1, 'minutes').format(); + + opts.created_at['$gte'].should.greaterThan(low); + opts.created_at['$gte'].should.lessThan(high); + }); + + it('should not enforce date filter if query includes id', function ( ) { + var opts = query({ find: { _id: 1234 } }); + + (typeof opts.date).should.equal('undefined') + }); +});