如何在ASP.NET上使用MongoDB搜索索引 /索引

发布于 2025-02-11 19:42:16 字数 1741 浏览 1 评论 0原文

当我使用一个简单的发现检索所有数据时,我收集了10 000个文档,(〜6 MB),

它需要一分钟以上的时间才能检索所有文档。

我在MongoDB上创建了一个搜索索引,以加快我想实现的目标。

我也有一个搜索索引:

”在此处输入图像说明“

这是此搜索索引的查询语法:

new BsonArray {
  new BsonDocument("$search", new BsonDocument {
    {
      "index",
      "searchProd"
    },
    {
      "text",
      new BsonDocument {
        {
          "query",
          application // DYNAMIC Value that I want to pass
        },
        {
          "path",
          new BsonDocument("wildcard", "*")
        }
      }
    }
  })
}

找不到如何在我的函数上实现它的方法。

public async Task<List<Produit>> ListAllProducts(string application)
{
    var sw   = Stopwatch.StartNew();
    var allProducts =  await _produits.Find(new BsonDocument("$search", new BsonDocument
    {
        {
            "index",
            "searchProd"
        },
        {
            "text",
            new BsonDocument
            {
                {
                    "query",
                    application
                },
                {
                    "path",
                    new BsonDocument("wildcard", "*")
                }
            }
        }
    })).ToList();
    return  allProducts;

}

通过这样做,我得到了

mongodb.driver.mongocommandexception:命令查找失败:未知顶部 电平操作员:$搜索。如果您的字段名称以一个开始 '$'符号,考虑使用$ getfield或$ setfield ..

谢谢。

I have a collection of 10 000 documents, (~ 6 Mb)

When I'm using a simple find to retrieve all the data, it takes like more than a minute to retrieve all the documents.

I created a search Index on MongoDb to speed up what I want to achieve.

enter image description here

I have also a search Index :

enter image description here

Here is the Query Syntax of this Search Index :

new BsonArray {
  new BsonDocument("$search", new BsonDocument {
    {
      "index",
      "searchProd"
    },
    {
      "text",
      new BsonDocument {
        {
          "query",
          application // DYNAMIC Value that I want to pass
        },
        {
          "path",
          new BsonDocument("wildcard", "*")
        }
      }
    }
  })
}

Couldn't find a way how to implement this on my function.

public async Task<List<Produit>> ListAllProducts(string application)
{
    var sw   = Stopwatch.StartNew();
    var allProducts =  await _produits.Find(new BsonDocument("$search", new BsonDocument
    {
        {
            "index",
            "searchProd"
        },
        {
            "text",
            new BsonDocument
            {
                {
                    "query",
                    application
                },
                {
                    "path",
                    new BsonDocument("wildcard", "*")
                }
            }
        }
    })).ToList();
    return  allProducts;

}

By doing this I'm getting

MongoDB.Driver.MongoCommandException: Command find failed: unknown top
level operator: $search. If you have a field name that starts with a
'$' symbol, consider using $getField or $setField..

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

家住魔仙堡 2025-02-18 19:42:16

Atlas $ search是一个聚合阶段,而不是查找参数。它应该看起来与:

 _produits.Aggregate().AppendStage<BsonDocument>("your $search stage in string or BsonDocument form").ToList();

atlas $search is an aggregation stage, not find argument. It should look similar to:

 _produits.Aggregate().AppendStage<BsonDocument>("your $search stage in string or BsonDocument form").ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文