如何通过.NET在MongoDB中的子文档上创建索引

时间:2015-06-24 05:19:31

标签: .net mongodb indexing

是否有一种流畅的语法在“产品”文档的“评论”子文档的“作者”键上创建索引,就像我如何在“价格”键上创建索引一样?

var connStr = "mongodb://localhost:27017";
var client = new MongoClient(connStr);
var db = client.GetDatabase("store");
var col = db.GetCollection<Product>("products");

await col.Indexes.CreateOneAsync(Builders<Product>.IndexKeys.Descending(x => x.Price));
await col.Indexes.CreateOneAsync("{'reviews.author':1}");

1 个答案:

答案 0 :(得分:0)

我认为你所指的是以下内容不起作用:

await collection.Indexes.CreateOneAsync(Builders<Product>.IndexKeys.Ascending(x => x.Reviews.Authors));

它给出了编译时错误。

当我们设计IndexKeysBuilder时,显然我们错过了这种情况。我创建了一张JIRA票据来跟踪添加此功能:

https://jira.mongodb.org/browse/CSHARP-1340

感谢您引起我们的注意。