Skip to content

Commit 53e0136

Browse files
author
Mishig
authored
[Mongo] Optimize reports collection query (v2) (#834)
* [Mongo] Optimize `reports` collection query (#832) * [Mongo] Optimize `reports` collection query * perform reports query only once * rm unused async * keep both indices * correct Objectid comparison
1 parent c755352 commit 53e0136

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

src/lib/server/database.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,5 @@ client.on("open", () => {
8686
assistants.createIndex({ featured: 1, userCount: -1 }).catch(console.error);
8787
assistants.createIndex({ modelId: 1, userCount: -1 }).catch(console.error);
8888
reports.createIndex({ assistantId: 1 }).catch(console.error);
89+
reports.createIndex({ createdBy: 1, assistantId: 1 }).catch(console.error);
8990
});

src/routes/settings/+layout.server.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { collections } from "$lib/server/database";
22
import { ObjectId } from "mongodb";
33
import type { LayoutServerLoad } from "./$types";
4+
import type { Report } from "$lib/types/Report";
45

56
export const load = (async ({ locals, parent }) => {
67
const { settings } = await parent();
@@ -12,20 +13,22 @@ export const load = (async ({ locals, parent }) => {
1213
})
1314
.toArray();
1415

16+
let reportsByUser: string[] = [];
17+
const createdBy = locals.user?._id ?? locals.sessionId;
18+
if (createdBy) {
19+
const reports = await collections.reports
20+
.find<Pick<Report, "assistantId">>({ createdBy }, { projection: { _id: 0, assistantId: 1 } })
21+
.toArray();
22+
reportsByUser = reports.map((r) => r.assistantId.toString());
23+
}
24+
1525
return {
16-
assistants: await Promise.all(
17-
assistants.map(async (el) => ({
18-
...el,
19-
_id: el._id.toString(),
20-
createdById: undefined,
21-
createdByMe:
22-
el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
23-
reported:
24-
(await collections.reports.countDocuments({
25-
assistantId: el._id,
26-
createdBy: locals.user?._id ?? locals.sessionId,
27-
})) > 0,
28-
}))
29-
),
26+
assistants: assistants.map((el) => ({
27+
...el,
28+
_id: el._id.toString(),
29+
createdById: undefined,
30+
createdByMe: el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
31+
reported: reportsByUser.includes(el._id.toString()),
32+
})),
3033
};
3134
}) satisfies LayoutServerLoad;

src/routes/settings/assistants/[assistantId]/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export const actions: Actions = {
5757
report: async ({ request, params, locals, url }) => {
5858
// is there already a report from this user for this model ?
5959
const report = await collections.reports.findOne({
60-
assistantId: new ObjectId(params.assistantId),
6160
createdBy: locals.user?._id ?? locals.sessionId,
61+
assistantId: new ObjectId(params.assistantId),
6262
});
6363

6464
if (report) {

0 commit comments

Comments
 (0)