MongoDB Indexes.CreateOneAsync 使用 Azure DocumentDB 的异常
MongoDB Indexes.CreateOneAsync Exception using Azure DocumentDB
我正在尝试使用连接到 Azure DocumentDB 实例的 c# MongoDB 驱动程序创建多个唯一索引,但在尝试创建第二个唯一索引时收到以下异常:
MongoDB.Driver.MongoCommandException: '命令创建索引失败:消息:{"Errors":["The number of unique keys cannot be greater than 1."]}
我似乎找不到任何关于 Azure DocumentDB 集合的唯一键数的文档。请注意,使用实际 MongoDB 实例时不会发生此异常。
var keys = Builders<ProductEntity>.IndexKeys.Ascending(p => p.UPC);
var options = new CreateIndexOptions<ProductEntity>() { Name = "UX_UPC", Unique = true, Sparse = true };
var result = await _collection.Indexes.CreateOneAsync(keys, options);
keys = Builders<ProductEntity>.IndexKeys.Ascending(p => p.Manufacturer).Ascending(p => p.MPN);
options = new CreateIndexOptions<ProductEntity>() { Name = "UX_Manufacturer_MPN", Unique = true, Sparse = true };
result = await _collection.Indexes.CreateOneAsync(keys, options);
public class ProductEntity
{
public Guid Id { get; set; }
public string UPC { get; set; }
public string MPN { get; set; }
public string Manufacturer { get; set; }
}
从this blog,我们可以发现它目前不支持唯一索引。
General availability is just the beginning for all the features and improvements we have in stored for DocumentDB: API for MongoDB. In the near future, we will be releasing support for Unique indexes and a couple major performance improvements.
本帖讨论,你可以参考
我正在尝试使用连接到 Azure DocumentDB 实例的 c# MongoDB 驱动程序创建多个唯一索引,但在尝试创建第二个唯一索引时收到以下异常:
MongoDB.Driver.MongoCommandException: '命令创建索引失败:消息:{"Errors":["The number of unique keys cannot be greater than 1."]}
我似乎找不到任何关于 Azure DocumentDB 集合的唯一键数的文档。请注意,使用实际 MongoDB 实例时不会发生此异常。
var keys = Builders<ProductEntity>.IndexKeys.Ascending(p => p.UPC);
var options = new CreateIndexOptions<ProductEntity>() { Name = "UX_UPC", Unique = true, Sparse = true };
var result = await _collection.Indexes.CreateOneAsync(keys, options);
keys = Builders<ProductEntity>.IndexKeys.Ascending(p => p.Manufacturer).Ascending(p => p.MPN);
options = new CreateIndexOptions<ProductEntity>() { Name = "UX_Manufacturer_MPN", Unique = true, Sparse = true };
result = await _collection.Indexes.CreateOneAsync(keys, options);
public class ProductEntity
{
public Guid Id { get; set; }
public string UPC { get; set; }
public string MPN { get; set; }
public string Manufacturer { get; set; }
}
从this blog,我们可以发现它目前不支持唯一索引。
General availability is just the beginning for all the features and improvements we have in stored for DocumentDB: API for MongoDB. In the near future, we will be releasing support for Unique indexes and a couple major performance improvements.
本帖讨论