Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Mongoose 7.5 for better $vector support #128

Merged
merged 4 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"eslint": "8.47.0",
"jsdoc-babel": "^0.5.0",
"jsdoc-to-markdown": "^7.1.1",
"mongoose": "^7.4.0",
"mongoose": "^7.5.0",
"nyc": "^15.1.0",
"sinon": "15.2.0",
"ts-mocha": "^10.0.0",
Expand All @@ -94,7 +94,7 @@
"winston": "^3.7.2"
},
"peerDependencies": {
"mongoose": "^7.4.0"
"mongoose": "^7.5.0"
},
"engines": {
"node": ">=14.0.0"
Expand Down
49 changes: 30 additions & 19 deletions src/collections/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,36 @@ export class Collection {

async updateOne(filter: Record<string, any>, update: Record<string, any>, options?: UpdateOneOptions) {
return executeOperation(async (): Promise<JSONAPIUpdateResult> => {
const command = {
updateOne: {
filter,
update,
options
}
};
setDefaultIdForUpsert(command.updateOne);
const updateOneResp = await this.httpClient.executeCommand(command, updateOneInternalOptionsKeys);
const resp = {
modifiedCount: updateOneResp.status.modifiedCount,
matchedCount: updateOneResp.status.matchedCount,
acknowledged: true
} as JSONAPIUpdateResult;
if (updateOneResp.status.upsertedId) {
resp.upsertedId = updateOneResp.status.upsertedId;
resp.upsertedCount = 1;
}
return resp;
type UpdateOneCommand = {
updateOne: {
filter?: Record<string, any>,
sort?: SortOption,
update?: Record<string, any>,
options?: UpdateOneOptions
}
}
const command: UpdateOneCommand = {
updateOne: {
filter,
update,
options
}
};
if (options?.sort != null) {
command.updateOne.sort = options?.sort;
}
setDefaultIdForUpsert(command.updateOne);
const updateOneResp = await this.httpClient.executeCommand(command, updateOneInternalOptionsKeys);
const resp = {
modifiedCount: updateOneResp.status.modifiedCount,
matchedCount: updateOneResp.status.matchedCount,
acknowledged: true
} as JSONAPIUpdateResult;
if (updateOneResp.status.upsertedId) {
resp.upsertedId = updateOneResp.status.upsertedId;
resp.upsertedCount = 1;
}
return resp;
});
}

Expand Down
99 changes: 76 additions & 23 deletions tests/driver/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,16 +777,7 @@ describe(`Mongoose Model API level tests`, async () => {
);
let Vector: Model<any>;

beforeEach(async () => {
mongooseInstance = await getInstance();
const options = {
username: process.env.STARGATE_USERNAME,
password: process.env.STARGATE_PASSWORD,
authUrl: process.env.STARGATE_AUTH_URL,
logSkippedOptions: true
};
await mongooseInstance.connect(dbUri, options);

this.beforeEach(async function() {
await mongooseInstance.connection.dropCollection('vector');
Vector = mongooseInstance.model(
'Vector',
Expand All @@ -807,6 +798,15 @@ describe(`Mongoose Model API level tests`, async () => {
]);
});

it('supports updating $vector with save()', async function() {
const vector = await Vector.findOne({ name: 'Test vector 1' }).orFail();
vector.$vector = [1, 101];
await vector.save();

const { $vector } = await Vector.findOne({ name: 'Test vector 1' }).orFail();
assert.deepStrictEqual($vector, [1, 101]);
});

it('supports sort() with $meta with find()', async function() {
let res = await Vector.
find({}).
Expand All @@ -815,56 +815,109 @@ describe(`Mongoose Model API level tests`, async () => {

res = await Vector.
find({}).
select({ $vector: 0 }).
sort({ $vector: { $meta: [99, 1] } });
assert.deepStrictEqual(res.map(doc => doc.name), ['Test vector 2', 'Test vector 1']);
assert.deepStrictEqual(res.map(doc => doc.$vector), [undefined, undefined]);

res = await Vector.
find({}).
limit(999).
sort({ $vector: { $meta: [99, 1] } });
assert.deepStrictEqual(res.map(doc => doc.name), ['Test vector 2', 'Test vector 1']);

res = await Vector.
findOne({}).
sort({ $vector: { $meta: [99, 1] } });
assert.deepStrictEqual(res.name, 'Test vector 2');

await assert.rejects(
Vector.find().limit(1001).sort({ $vector: { $meta: [99, 1] } }),
/limit options should not be greater than 1000 for vector search/
);
});

it('supports sort() with $meta with findOne()', async function() {
let res = await Vector.
findOne({}).
sort({ $vector: { $meta: [1, 99] } });
assert.deepStrictEqual(res.name, 'Test vector 1');

res = await Vector.
findOne({}).
it('supports sort() with $meta with updateOne()', async function() {
await Vector.
updateOne(
{},
{ name: 'found vector', $vector: [990, 1] }
).
sort({ $vector: { $meta: [99, 1] } });
assert.deepStrictEqual(res.name, 'Test vector 2');
const vectors = await Vector.find().limit(20).sort({ name: 1 });
assert.deepStrictEqual(vectors.map(v => v.name), ['Test vector 1', 'found vector']);
assert.deepStrictEqual(vectors.map(v => v.$vector), [[1, 100], [990, 1]]);
});

it('supports sort() with $meta with findOneAndUpdate()', async function() {
const res = await Vector.
findOneAndUpdate({}, { name: 'found vector' }, { returnDocument: 'after' }).
findOneAndUpdate(
{},
{ name: 'found vector', $vector: [990, 1] },
{ returnDocument: 'before' }
).
sort({ $vector: { $meta: [99, 1] } });
assert.deepStrictEqual(res.$vector, [100, 1]);
assert.strictEqual(res.name, 'found vector');
assert.strictEqual(res.name, 'Test vector 2');

const doc = await Vector.findById(res._id);
assert.strictEqual(doc.name, 'found vector');
assert.deepStrictEqual(doc.$vector, [990, 1]);
});

it('supports $setOnInsert of $vector with findOneAndUpdate()', async function() {
let res = await Vector.
findOneAndUpdate(
{ name: 'Test vector 2' },
{ $setOnInsert: { $vector: [990, 1] } },
{ returnDocument: 'after', upsert: true }
);
assert.deepStrictEqual(res.$vector, [100, 1]);
assert.strictEqual(res.name, 'Test vector 2');

res = await Vector.
findOneAndUpdate(
{ name: 'Test vector 3' },
{ $setOnInsert: { $vector: [990, 1] } },
{ returnDocument: 'after', upsert: true }
);
assert.deepStrictEqual(res.$vector, [990, 1]);
assert.strictEqual(res.name, 'Test vector 3');
});

it('supports $unset of $vector with findOneAndUpdate()', async function() {
const res = await Vector.
findOneAndUpdate(
{ name: 'Test vector 2' },
{ $unset: { $vector: 1 } },
{ returnDocument: 'after' }
);
assert.deepStrictEqual(res.$vector, undefined);
assert.strictEqual(res.name, 'Test vector 2');
});

it('supports sort() with $meta with findOneAndReplace()', async function() {
const res = await Vector.
findOneAndReplace(
{},
{ name: 'found vector' },
{ name: 'found vector', $vector: [990, 1] },
{ returnDocument: 'before' }
).
sort({ $vector: { $meta: [99, 1] } });
assert.deepStrictEqual(res.$vector, [100, 1]);
assert.strictEqual(res.name, 'Test vector 2');

const doc = await Vector.findById(res._id);
assert.strictEqual(doc.name, 'found vector');
assert.deepStrictEqual(doc.$vector, [990, 1]);
});

it('supports sort() with $meta with findOneAndDelete()', async function() {
const res = await Vector.
findOneAndDelete({}, { name: 'found vector' }, { returnDocument: 'before' }).
findOneAndDelete(
{},
{ returnDocument: 'before' }
).
sort({ $vector: { $meta: [1, 99] } });
assert.deepStrictEqual(res.$vector, [1, 100]);
assert.strictEqual(res.name, 'Test vector 1');
Expand Down