2020-08-19 19:51:45 +02:00
|
|
|
/**
|
|
|
|
* Tests for aggregation requests with the collectionUUID parameter.
|
|
|
|
* @tags: [
|
|
|
|
* # Change stream aggregations don't support read concerns other than 'majority'
|
|
|
|
* assumes_read_concern_unchanged,
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
(function() {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
load("jstests/libs/fixture_helpers.js"); // For 'isMongos'
|
|
|
|
|
|
|
|
const dbName = jsTestName();
|
|
|
|
const collName = "foo";
|
|
|
|
|
|
|
|
const testDB = db.getSiblingDB(dbName);
|
|
|
|
const testColl = testDB.getCollection(collName);
|
|
|
|
|
2022-02-15 06:53:54 +01:00
|
|
|
const validateErrorResponse = function(
|
|
|
|
res, db, collectionUUID, expectedCollection, actualCollection) {
|
|
|
|
assert.eq(res.db, db);
|
2022-02-02 18:19:31 +01:00
|
|
|
assert.eq(res.collectionUUID, collectionUUID);
|
2022-02-15 06:53:54 +01:00
|
|
|
assert.eq(res.expectedCollection, expectedCollection);
|
|
|
|
assert.eq(res.actualCollection, actualCollection);
|
2022-02-02 18:19:31 +01:00
|
|
|
};
|
|
|
|
|
2020-08-19 19:51:45 +02:00
|
|
|
const docs = [{_id: 1}, {_id: 2}];
|
|
|
|
|
2022-03-31 20:06:42 +02:00
|
|
|
assert.commandWorked(testDB.dropDatabase());
|
2020-08-19 19:51:45 +02:00
|
|
|
assert.commandWorked(testColl.insert(docs));
|
|
|
|
|
|
|
|
// Get the namespace's initial UUID.
|
|
|
|
let collectionInfos = testDB.getCollectionInfos({name: collName});
|
|
|
|
let uuid = collectionInfos[0].info.uuid;
|
|
|
|
assert(uuid, "Expected collection " + collName + " to have a UUID.");
|
|
|
|
|
|
|
|
// An aggregation with the UUID should succeed and find the same documents as an aggregation with
|
|
|
|
// the collection name.
|
|
|
|
let uuidRes = assert.commandWorked(testDB.runCommand(
|
|
|
|
{aggregate: collName, collectionUUID: uuid, pipeline: [{$match: {}}], cursor: {}}));
|
|
|
|
assert.sameMembers(uuidRes.cursor.firstBatch, docs);
|
|
|
|
|
|
|
|
let collNameRes = assert.commandWorked(
|
|
|
|
testDB.runCommand({aggregate: collName, pipeline: [{$match: {}}], cursor: {}}));
|
|
|
|
assert.sameMembers(collNameRes.cursor.firstBatch, uuidRes.cursor.firstBatch);
|
|
|
|
|
|
|
|
// getMore should work with cursors created by an aggregation with a uuid.
|
|
|
|
uuidRes = assert.commandWorked(testDB.runCommand(
|
|
|
|
{aggregate: collName, pipeline: [{$match: {}}, {$sort: {_id: 1}}], cursor: {batchSize: 1}}));
|
|
|
|
assert.eq(1, uuidRes.cursor.firstBatch.length, tojson(uuidRes));
|
|
|
|
assert.eq(docs[0], uuidRes.cursor.firstBatch[0], tojson(uuidRes));
|
|
|
|
|
|
|
|
const getMoreRes =
|
|
|
|
assert.commandWorked(testDB.runCommand({getMore: uuidRes.cursor.id, collection: collName}));
|
|
|
|
assert.eq(1, getMoreRes.cursor.nextBatch.length, tojson(getMoreRes));
|
|
|
|
assert.eq(docs[1], getMoreRes.cursor.nextBatch[0], tojson(getMoreRes));
|
|
|
|
assert.eq(0, getMoreRes.cursor.id, tojson(getMoreRes));
|
|
|
|
|
|
|
|
// An aggregation with collectionUUID throws NamespaceNotFound if the namespace does not exist, even
|
|
|
|
// if a collection does exist with the given uuid.
|
2022-02-02 18:19:31 +01:00
|
|
|
let res = assert.commandFailedWithCode(
|
2020-08-19 19:51:45 +02:00
|
|
|
testDB.runCommand(
|
|
|
|
{aggregate: "doesNotExist", collectionUUID: uuid, pipeline: [{$match: {}}], cursor: {}}),
|
2022-02-02 18:19:31 +01:00
|
|
|
ErrorCodes.CollectionUUIDMismatch);
|
2022-02-15 06:53:54 +01:00
|
|
|
validateErrorResponse(res, dbName, uuid, 'doesNotExist', testColl.getName());
|
2020-08-19 19:51:45 +02:00
|
|
|
|
|
|
|
// Drop the collection.
|
|
|
|
testColl.drop({writeConcern: {w: "majority"}});
|
|
|
|
|
|
|
|
// An aggregation with the initial UUID should fail since the namespace doesn't exist.
|
2022-02-02 18:19:31 +01:00
|
|
|
res = assert.commandFailedWithCode(
|
2020-08-19 19:51:45 +02:00
|
|
|
testDB.runCommand(
|
|
|
|
{aggregate: collName, collectionUUID: uuid, pipeline: [{$match: {}}], cursor: {}}),
|
2022-02-02 18:19:31 +01:00
|
|
|
ErrorCodes.CollectionUUIDMismatch);
|
2022-02-15 06:53:54 +01:00
|
|
|
validateErrorResponse(res, dbName, uuid, testColl.getName(), null);
|
2020-08-19 19:51:45 +02:00
|
|
|
|
|
|
|
// Now recreate the collection.
|
|
|
|
assert.commandWorked(testColl.insert(docs));
|
|
|
|
|
|
|
|
// An aggregation with the initial UUID should still fail despite the namespace existing.
|
2022-02-02 18:19:31 +01:00
|
|
|
res = assert.commandFailedWithCode(
|
2020-08-19 19:51:45 +02:00
|
|
|
testDB.runCommand(
|
|
|
|
{aggregate: collName, collectionUUID: uuid, pipeline: [{$match: {}}], cursor: {}}),
|
2022-02-02 18:19:31 +01:00
|
|
|
ErrorCodes.CollectionUUIDMismatch);
|
2022-02-15 06:53:54 +01:00
|
|
|
validateErrorResponse(res, dbName, uuid, testColl.getName(), null);
|
2020-08-19 19:51:45 +02:00
|
|
|
|
|
|
|
collNameRes = assert.commandWorked(
|
|
|
|
testDB.runCommand({aggregate: collName, pipeline: [{$match: {}}], cursor: {}}));
|
|
|
|
assert.sameMembers(collNameRes.cursor.firstBatch, docs);
|
|
|
|
|
2022-02-02 18:19:31 +01:00
|
|
|
// An aggregation with a collectionUUID should fail with CollectionUUIDMismatch if the namespace is
|
|
|
|
// a view.
|
2022-03-31 20:06:42 +02:00
|
|
|
const viewName = 'view';
|
|
|
|
assert.commandWorked(testDB.runCommand(
|
|
|
|
{create: viewName, viewOn: testColl.getName(), pipeline: [], writeConcern: {w: "majority"}}));
|
2022-02-02 18:19:31 +01:00
|
|
|
|
2022-02-10 01:55:18 +01:00
|
|
|
res = assert.commandFailedWithCode(
|
2022-02-02 18:19:31 +01:00
|
|
|
testDB.runCommand(
|
|
|
|
{aggregate: "viewCollection", collectionUUID: uuid, pipeline: [{$match: {}}], cursor: {}}),
|
|
|
|
ErrorCodes.CollectionUUIDMismatch);
|
2022-02-15 06:53:54 +01:00
|
|
|
validateErrorResponse(res, dbName, uuid, 'viewCollection', null);
|
2022-02-02 18:19:31 +01:00
|
|
|
|
2020-08-19 19:51:45 +02:00
|
|
|
//
|
|
|
|
// Tests for rejecting invalid collectionUUIDs and cases where collectionUUID is not allowed.
|
|
|
|
//
|
|
|
|
|
|
|
|
// collectionUUID must be a UUID.
|
|
|
|
assert.commandFailedWithCode(
|
|
|
|
testDB.runCommand(
|
|
|
|
{aggregate: collName, collectionUUID: "NotAUUID", pipeline: [{$match: {}}], cursor: {}}),
|
2020-10-29 13:14:01 +01:00
|
|
|
ErrorCodes.TypeMismatch);
|
2020-08-19 19:51:45 +02:00
|
|
|
|
|
|
|
// collectionUUID is not allowed with change streams.
|
|
|
|
assert.commandFailedWithCode(
|
|
|
|
testDB.runCommand(
|
|
|
|
{aggregate: collName, collectionUUID: uuid, pipeline: [{$changeStream: {}}], cursor: {}}),
|
|
|
|
4928900);
|
|
|
|
|
|
|
|
// collectionUUID is not allowed with collectionless aggregations.
|
|
|
|
assert.commandFailedWithCode(
|
2022-03-31 20:06:42 +02:00
|
|
|
testDB.adminCommand(
|
|
|
|
{aggregate: 1, collectionUUID: uuid, pipeline: [{$currentOp: {}}], cursor: {}}),
|
2020-08-19 19:51:45 +02:00
|
|
|
4928901);
|
|
|
|
})();
|