Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 43 additions & 64 deletions test/integration/crud/find.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
'use strict';
const { assert: test } = require('../shared');
const { assert: test, filterForCommands } = require('../shared');
const { expect } = require('chai');
const sinon = require('sinon');
const { setTimeout } = require('timers');
const { Code, ObjectId, Long, Binary, ReturnDocument, CursorResponse } = require('../../mongodb');
const {
Code,
ObjectId,
Long,
Binary,
ReturnDocument,
CursorResponse,
MongoServerError
} = require('../../mongodb');

describe('Find', function () {
/** @type(import('../../mongodb').MongoClient */
let client;

beforeEach(async function () {
Expand Down Expand Up @@ -496,70 +505,40 @@ describe('Find', function () {
}
});

it('shouldCorrectlyPerformFindsWithHintTurnedOn', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
it('shouldCorrectlyPerformFindsWithHintTurnedOn', async function () {
const configuration = this.configuration;
client = configuration.newClient(configuration.writeConcernMax(), {
monitorCommands: true
});

test: function (done) {
var configuration = this.configuration;
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
client.connect(function (err, client) {
var db = client.db(configuration.db);
db.createCollection('test_hint', function (err, collection) {
collection.insert({ a: 1 }, configuration.writeConcernMax(), function (err) {
expect(err).to.not.exist;
db.createIndex(
collection.collectionName,
'a',
configuration.writeConcernMax(),
function (err) {
expect(err).to.not.exist;
collection.find({ a: 1 }, { hint: 'a' }).toArray(function (err) {
test.ok(err != null);
const finds = [];
client.on('commandStarted', filterForCommands('find', finds));

collection.find({ a: 1 }, { hint: ['a'] }).toArray(function (err, items) {
expect(err).to.not.exist;
test.equal(1, items.length);

collection.find({ a: 1 }, { hint: { a: 1 } }).toArray(function (err, items) {
test.equal(1, items.length);

// Modify hints
collection.hint = 'a_1';
test.equal('a_1', collection.hint);
collection.find({ a: 1 }).toArray(function (err, items) {
test.equal(1, items.length);

collection.hint = ['a'];
test.equal(1, collection.hint['a']);
collection.find({ a: 1 }).toArray(function (err, items) {
test.equal(1, items.length);

collection.hint = { a: 1 };
test.equal(1, collection.hint['a']);
collection.find({ a: 1 }).toArray(function (err, items) {
test.equal(1, items.length);

collection.hint = null;
test.ok(collection.hint == null);
collection.find({ a: 1 }).toArray(function (err, items) {
test.equal(1, items.length);
// Let's close the db
client.close(done);
});
});
});
});
});
});
});
}
);
});
});
});
}
await client.connect();

const db = client.db(configuration.db);
const collection = await db.createCollection('test_hint');

await collection.deleteMany({});
await collection.insert({ a: 1 }, configuration.writeConcernMax());

await db.createIndex(collection.collectionName, 'a', configuration.writeConcernMax());

expect(
await collection
.find({ a: 1 }, { hint: 'a' })
.toArray()
.catch(e => e)
).to.be.instanceOf(MongoServerError);
expect(finds[0].command.hint).to.equal('a');

// Test with hint as array
expect(await collection.find({ a: 1 }, { hint: ['a'] }).toArray()).to.have.lengthOf(1);
expect(finds[1].command.hint).to.deep.equal({ a: 1 });

// Test with hint as object
expect(await collection.find({ a: 1 }, { hint: { a: 1 } }).toArray()).to.have.lengthOf(1);
expect(finds[2].command.hint).to.deep.equal({ a: 1 });
});

it('shouldCorrectlyPerformFindByObjectId', {
Expand Down