Add Mongo hint and maxTimeMS options (#5932)

This commit is contained in:
Brenton Partridge
2017-05-12 18:35:41 -04:00
parent d8ddeb7047
commit 75a54ece56
2 changed files with 55 additions and 0 deletions

View File

@@ -102,3 +102,51 @@ Tinytest.add('collection - call native find with sort function',
}
}
);
Tinytest.add('collection - calling native find with maxTimeMS should timeout',
function(test) {
var collectionName = 'findOptions1' + test.id;
var collection = new Mongo.Collection(collectionName);
collection.insert({a: 1});
function doTest() {
return collection.find({$where: "sleep(100) || true"}, {maxTimeMS: 50}).count();
}
if (Meteor.isServer) {
test.throws(doTest);
}
}
);
Tinytest.add('collection - calling native find with $reverse hint should reverse on server',
function(test) {
var collectionName = 'findOptions2' + test.id;
var collection = new Mongo.Collection(collectionName);
collection.insert({a: 1});
collection.insert({a: 2});
function m(doc) { return doc.a; }
var fwd = collection.find({}, {hint: {$natural: 1}}).map(m);
var rev = collection.find({}, {hint: {$natural: -1}}).map(m);
if (Meteor.isServer) {
test.equal(fwd, rev.reverse());
} else {
// NOTE: should be documented that hints don't work on client
test.equal(fwd, rev);
}
}
);
Tinytest.add('collection - calling native find with good hint and maxTimeMS should succeed',
function(test) {
var collectionName = 'findOptions3' + test.id;
var collection = new Mongo.Collection(collectionName);
collection.insert({a: 1});
if (Meteor.isServer) {
collection.rawCollection().createIndex({a: 1});
}
test.equal(collection.find({}, {hint: {a: 1}, maxTimeMS: 1000}).count(), 1);
}
);

View File

@@ -992,6 +992,13 @@ MongoConnection.prototype._createSynchronousCursor = function(
replaceTypes(cursorDescription.selector, replaceMeteorAtomWithMongo),
cursorOptions.fields, mongoOptions);
if (typeof cursorOptions.maxTimeMS !== 'undefined') {
dbCursor = dbCursor.maxTimeMS(cursorOptions.maxTimeMS);
}
if (typeof cursorOptions.hint !== 'undefined') {
dbCursor = dbCursor.hint(cursorOptions.hint);
}
return new SynchronousCursor(dbCursor, cursorDescription, options);
};