import * as mongo from 'mongodb';
/**
* Run with:
* export MONGO_TEST_URL='mongodb:
* node script.mjs
*/
const MONGO_URL = process.env.MONGO_TEST_URL ?? 'mongodb:;
const client = new mongo.MongoClient(MONGO_URL);
const db = client.db();
const clusteredName = `clustered_repro`;
const plainName = `plain_repro`;
await client.connect();
await using _ = { [Symbol.asyncDispose]: async () => await client.close() };
const buildInfo = await db.command({ buildInfo: 1 });
console.log(`MongoDB version: ${buildInfo.version}`);
console.log(`Database: ${MONGO_URL}`);
await db
.collection(clusteredName)
.drop()
.catch(() => {});
await db
.collection(plainName)
.drop()
.catch(() => {});
await db.createCollection(clusteredName, {
clusteredIndex: { name: '_id', unique: true, key: { _id: 1 } }
});
await db.createCollection(plainName);
const clustered = db.collection(clusteredName);
const plain = db.collection(plainName);
const docs = [{ _id: 1 }, { _id: 2 }, { _id: 3 }, { _id: 4 }];
await clustered.insertMany(docs);
await plain.insertMany(docs);
const plainResults1 = await plain
.find(
{
_id: {
$lt: 3
}
},
{ sort: { _id: -1 } }
)
.toArray();
const plainResults2 = await plain
.aggregate([
{
$match: {
_id: {
$lt: 3
}
}
},
{ $sort: { _id: -1 } }
])
.toArray();
const clusteredResults1 = await clustered
.find(
{
_id: {
$lt: 3
}
},
{ sort: { _id: -1 } }
)
.toArray();
const clusteredResults2 = await clustered
.aggregate([
{
$match: {
_id: {
$lt: 3
}
}
},
{ $sort: { _id: -1 } }
])
.toArray();
const clusteredResults3 = await clustered
.aggregate([
{
$match: {
_id: {
$lt: 3
}
}
},
{ $sort: { _id: 1 } }
])
.toArray();
const clusteredResults4 = await clustered
.find(
{
_id: {
$lte: 2
}
},
{ sort: { _id: -1 } }
)
.toArray();
const clusteredResults5 = await clustered
.aggregate([
{
$match: {
_id: {
$lte: 2
}
}
},
{ $sort: { _id: -1 } }
])
.toArray();
console.log('These should all filter out { _id: 3 }');
console.log('Plain 1:', plainResults1, '# find');
console.log('Plain 2:', plainResults2, '# aggregate');
console.log('Clustered 1:', clusteredResults1, '# find');
console.log('Clustered 2:', clusteredResults2, '# aggregate, sort _id: -1');
console.log('Clustered 3:', clusteredResults3, '# aggregate, sort _id: 1');
console.log('Clustered 4:', clusteredResults4, '# find, sort _id: -1, $lte 2');
console.log('Clustered 5:', clusteredResults5, '# aggregate, sort _id: -1, $lte 2');