【问题标题】:how to search on mongodb by using runcommand如何使用 runco​​mmand 在 mongodb 上进行搜索
【发布时间】:2014-08-04 20:38:32
【问题描述】:

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

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

        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
        }

【问题讨论】:

  • 请澄清您的问题。你想做什么,你的预期结果是什么,出了什么问题,如果出了问题?另外,请在您的问题中修复代码formatting
  • 您是否考虑过改用 AsQueryable 并使用 Linq 进行搜索?
  • mclaassen 但是如何通过 linq 在 mongodb 中搜索??
  • 如果我在 textbox1 中插入“fox”之类的单词,我的预期结果是正确的搜索结果,但我没有得到任何结果

标签: c# mongodb full-text-search nosql


【解决方案1】:

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

【讨论】:

  • 谢谢 ^_^ 谢谢 :) 谢谢 Eng mclaassen :)
猜你喜欢
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-03
相关资源
最近更新 更多