首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在mongodb中选择此查询?

如何在mongodb中选择此查询?
EN

Stack Overflow用户
提问于 2022-09-30 09:39:55
回答 1查看 66关注 0票数 0

以下是完整的查询:

代码语言:javascript
复制
  async getHistoryByTag(context: StandardContext) {
    const { mongo } = context.state;
    const tag = context.params.tag.replace(/-+/g, " ");
    const today = new Date();
    const prevSunday = getOneWeekAgo(today);

    console.log("today: ", today, "prev sunday: ", prevSunday);

    const all = await mongo
      .collection("search_history")
      .aggregate<SearchHistoryModel & { username: string; word: string }>([
        {
          $match: {
            created_at: { $gt: prevSunday.toISOString() },
          },
        },
        {
          $lookup: {
            from: "users",
            localField: "user_id",
            foreignField: "id",
            as: "user",
            pipeline: [
              {
                $project: {
                  username: 1,
                },
              },
            ],
          },
        },

        // { search_id: 1, created_at: 1, url: 1 }
        {
          $lookup: {
            from: "positive",
            localField: "search_id",
            foreignField: "search_id",
            as: "positive",
            pipeline: [
              {
                $project: {
                  word: 1,
                },
              },
            ],
          },
        },
        {
          $unwind: {
            path: "$user",
            preserveNullAndEmptyArrays: true,
          },
        },
        {
          $unwind: {
            path: "$positive",
            preserveNullAndEmptyArrays: true,
          },
        },
        {
          $replaceRoot: {
            newRoot: {
              $mergeObjects: ["$$ROOT", "$user", "$positive"],
            },
          },
        },
        {
          $match: {
            word: tag,
          },
        },
        {
          $project: {
            user: 0,
            positive: 0,
          },
        },
        {
          $sort: {
            created_at: -1,
          },
        },
      ])
      .toArray();

    context.response.body = all.filter((value, index, self) => {
      return self.findIndex((v) => v.url === value.url) === index;
    });
  }

我尝试过添加索引,但仍然需要5-10秒才能做出响应。

EN

回答 1

Stack Overflow用户

发布于 2022-09-30 14:49:48

你只需要重新订购蒙戈管道一点。

匹配和排序应该首先使用索引,也不要在根管道中使用填充属性的匹配,而是在填充管道中使用匹配。

所以,你的管道应该是这样的。

代码语言:javascript
复制
    const all = await mongo
      .collection("search_history")
      .aggregate<SearchHistoryModel & { username: string; word: string }>([
        // match should be first
        {
          $match: {
            created_at: { $gt: prevSunday.toISOString() },
          },
        },
        // sort should be second
        {
          $sort: {
            created_at: -1,
          },
        },
        {
          $lookup: {
            from: "users",
            localField: "user_id",
            foreignField: "id",
            as: "user",
            pipeline: [
              {
                $project: {
                  username: 1,
                },
              },
            ],
          },
        },
        {
          $lookup: {
            from: "positive",
            localField: "search_id",
            foreignField: "search_id",
            as: "positive",
            pipeline: [
              {    
                $match: {word: tag},   // move the populated match inside the populate pipeline to use the index
              }
              {
                $project: {
                  word: 1,
                },
              },
            ],
          },
        },
        {
          $unwind: {
            path: "$user",
            preserveNullAndEmptyArrays: true,
          },
        },
        {
          $unwind: {
            path: "$positive",
            preserveNullAndEmptyArrays: true,
          },
        },
        {
          $replaceRoot: {
            newRoot: {
              $mergeObjects: ["$$ROOT", "$user", "$positive"],
            },
          },
        },
        {
          $project: {
            user: 0,
            positive: 0,
          },
        },
      ])
      .toArray();

注意:如果您的收集是巨大的,使用分页更快的查询。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73906772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档