Просмотр исходного кода

Merge pull request #8985 from weseek/fix/aggregation-for-mongo44

fix: Regaining lost backward compatibility for MongoDB 4.4
Yuki Takei 1 год назад
Родитель
Сommit
e6eed729f3
1 измененных файлов с 23 добавлено и 2 удалено
  1. 23 2
      apps/app/src/server/service/search-delegator/aggregate-to-index.ts

+ 23 - 2
apps/app/src/server/service/search-delegator/aggregate-to-index.ts

@@ -51,11 +51,32 @@ export const aggregatePipelineToIndex = (maxBodyLengthToIndex: number, query?: Q
 
 
     // join Comment
     // join Comment
     {
     {
+      // MongoDB 5.0 or later can use concise syntax
+      // https://www.mongodb.com/docs/v6.0/reference/operator/aggregation/lookup/#correlated-subqueries-using-concise-syntax
+      // $lookup: {
+      //   from: 'comments',
+      //   localField: '_id',
+      //   foreignField: 'page',
+      //   pipeline: [
+      //     {
+      //       $addFields: {
+      //         commentLength: { $strLenCP: '$comment' },
+      //       },
+      //     },
+      //   ],
+      //   as: 'comments',
+      // },
       $lookup: {
       $lookup: {
         from: 'comments',
         from: 'comments',
-        localField: '_id',
-        foreignField: 'page',
+        let: { pageId: '$_id' },
         pipeline: [
         pipeline: [
+          {
+            $match: {
+              $expr: {
+                $eq: ['$page', '$$pageId'],
+              },
+            },
+          },
           {
           {
             $addFields: {
             $addFields: {
               commentLength: { $strLenCP: '$comment' },
               commentLength: { $strLenCP: '$comment' },