简体   繁体   English

使用文本索引在聚合管道中填充的 Mongo 搜索

[英]Mongo search with populate in aggregation pipeline with text index

I have one query regarding search implementation.我有一个关于搜索实施的查询。 Here search implementation or atlas-search is best suitable for search in a single collection.这里的搜索实现或 atlas-search 最适合在单个集合中搜索。 I think normally we faced a scenario where we need to search with populate or ref collection also.我认为通常我们面临一个场景,我们也需要使用填充或参考集合进行搜索。 How can I do it with better implementation?我怎样才能更好地实施呢?

Consider the very simple scenario where I have 2 collections .考虑一个非常简单的场景,我有 2 个collections

1 Product (text index of product name field) 1产品(产品名称字段的文本索引)

2 User (text index on username fields may be firstName, lastName etc) 2用户(用户名字段上的文本索引可能是名字、姓氏等)

The user creates the products so in product collection there is ref on createdBy with user's MongoID.用户创建产品,因此在产品集合中, createdBy上有用户的 MongoID 参考。 On Frontend, there is a listing of products so I need to provide search criteria like below in single text search在前端,有一个产品列表,所以我需要在单文本搜索中提供如下搜索条件

  1. I can search with product name我可以用产品名称搜索
  2. I can search with the name of the owner of the product我可以用产品所有者的名字搜索

What is the best way to handle such a scenario with MongoAtlas?使用 MongoAtlas 处理这种情况的最佳方法是什么? ex.前任。 I want all products created by "Donni Bachhan".我想要“Donni Bachhan”创建的所有产品。 what is the aggregation pipeline you just or best practise for it您刚刚的聚合管道或最佳实践是什么

I have already asked the question with MongoDB community but not get proper help, please review below link for the community reference我已经向 MongoDB 社区提出了问题,但没有得到适当的帮助,请查看以下链接以获取社区参考

https://developer.mongodb.com/community/forums/t/mongo-search-with-populate-in-aggregation-pipeline-with-text-index/6953 https://developer.mongodb.com/community/forums/t/mongo-search-with-populate-in-aggregation-pipeline-with-text-index/6953

Better I will explain it as steps更好的是,我将其解释为步骤

Step 1: match with user collections: There you will get user details第 1 步:与用户 collections 匹配:在那里您将获得用户详细信息

Step 2: lookup with product collections: with that, you will get the user-created all products.第 2 步:查找产品 collections:这样,您将获得用户创建的所有产品。

Step 3: match for search, if there any search or filter needed to match this with product name第 3 步:匹配搜索,如果需要任何搜索或过滤器将其与产品名称匹配

example: (Hopefully this may help you)示例:(希望这可以帮助你)

    async function search(userid, query = null)
    {
      return await userModel
        .aggregate()
        .match({
          _id: userid
        })
        .lookup({
          from: "products",
          localField: "_id",
          foreignField: "userid",
          as: "products"
        })
        .match({ $text: { $search: "cake" }  })
        .exec();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM