gpt4 book ai didi

c# - 如何使用runco​​mmand在mongodb上搜索

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:44 24 4
gpt4 key购买 nike

这是我的代码...但是 runco​​mmand 没有得到任何结果

注意:“标题”是一个索引字段

        var mongoClient = new MongoClient(ConfigurationManager.AppSettings["connectionString"]);

var server = mongoClient.GetServer();
mongodatabase = server.GetDatabase("Htmlattachment2");
MongoCollection<Department> attachments = mongodatabase.GetCollection<Department>("attachment2");

attachments.EnsureIndex(new IndexKeysBuilder().Ascending("title"));
attachments.Insert(new BsonDocument("title", "The quick brown fox"));

var t = attachments.GetIndexes().ToList();
//foreach (Attachements emp in attachments.FindAll())
//{ }

var textSearchCommand = new CommandDocument
{
{ "text","title" },
{ "search", textBox1.Text }
};

var commandResult = mongodatabase.RunCommand(textSearchCommand);

var response = commandResult.Response["results"].AsBsonArray;
foreach (BsonDocument result in response)
{
// process result
}

最佳答案

如果您只使用 LINQ 执行查询,可能会更容易。有一个关于如何将 LINQ 与 MongoDB 结合使用的教程 here .请注意,MongoDB C# 驱动程序将 LINQ 查询转换为在数据库上作为 native Mongo 查询运行。

关于c# - 如何使用runco​​mmand在mongodb上搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25127130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com