这是我的代码...但是 runcommand 没有得到任何结果
注意:“标题”是一个索引字段
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 查询运行。
我是一名优秀的程序员,十分优秀!