如何通过 .NET 在 MongoDB 中的子文档上创建索引
How to create indexes on sub-documents in MongoDB via .NET
是否有一种流畅的语法可以在 "product" 文档的 "reviews" 子文档的 "author" 键上创建索引,就像我如何在 "product" 上创建索引一样"price" 键?
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}");
我假设您指的是以下内容不起作用:
await collection.Indexes.CreateOneAsync(Builders<Product>.IndexKeys.Ascending(x => x.Reviews.Authors));
编译时报错
显然我们在设计 IndexKeysBuilder 时错过了这种情况。我创建了一个 JIRA 票证来跟踪添加此功能:
https://jira.mongodb.org/browse/CSHARP-1340
感谢您提醒我们注意此事。
是否有一种流畅的语法可以在 "product" 文档的 "reviews" 子文档的 "author" 键上创建索引,就像我如何在 "product" 上创建索引一样"price" 键?
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}");
我假设您指的是以下内容不起作用:
await collection.Indexes.CreateOneAsync(Builders<Product>.IndexKeys.Ascending(x => x.Reviews.Authors));
编译时报错
显然我们在设计 IndexKeysBuilder 时错过了这种情况。我创建了一个 JIRA 票证来跟踪添加此功能:
https://jira.mongodb.org/browse/CSHARP-1340
感谢您提醒我们注意此事。