diff --git a/lib/mongo-milestone-db.js b/lib/mongo-milestone-db.js index 3d625de..5111ede 100644 --- a/lib/mongo-milestone-db.js +++ b/lib/mongo-milestone-db.js @@ -111,20 +111,27 @@ class MongoMilestoneDB extends MilestoneDB { return Promise.reject(new InvalidTimestampError()); } - const nullSortOrder = isAfterTimestamp ? -1 : 1; + const limit = 1; + let query; + let sortOrder; + + if (timestamp === null) { + query = {d: id}; + sortOrder = isAfterTimestamp ? -1 : 1; + } else { + const comparator = isAfterTimestamp ? {$gte: timestamp} : {$lte: timestamp}; + query = { + d: id, + 'm.mtime': comparator, + }; + sortOrder = isAfterTimestamp ? 1 : -1; + } - const query = {d: id}; const options = { - limit: 1, - sort: {'m.mtime': nullSortOrder}, + limit: limit, + sort: {'m.mtime': sortOrder}, }; - if (timestamp !== null) { - const comparator = isAfterTimestamp ? {$gte: timestamp} : {$lte: timestamp}; - query['m.mtime'] = comparator; - options.sort['m.mtime'] = -nullSortOrder; - } - return this._getMilestoneSnapshotByQuery(collectionName, query, options); }