【发布时间】:2017-04-17 21:01:23
【问题描述】:
我正在尝试使用连接到 Azure DocumentDB 实例的 c# MongoDB 驱动程序创建多个唯一索引,但在尝试创建第二个唯一索引时收到以下异常:
MongoDB.Driver.MongoCommandException: 'Command createIndexes failed: Message: {"Errors":["唯一键的数量不能大于 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; }
}
【问题讨论】:
-
请分享类
ProductEntity的定义,我们将根据您的代码重现问题。 -
您好 Fred,我按照您的要求添加了类定义。如果您需要更多信息来重现该问题,请告诉我。谢谢!
标签: c# mongodb azure azure-cosmosdb mongodb-indexes