我正在尝试使用连接到Azure DocumentDB实例的c#MongoDB驱动程序创建多个唯一索引,但在尝试创建第二个唯一索引时,我收到以下异常:
MongoDB.Driver.MongoCommandException:'命令createIndexes失败:消息:{“错误”:[“唯一键的数量不能大于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; }
}
1 回答
从this blog,我们发现它目前不支持 Unique indexes .
这个帖子讨论了Cannot create index in Azure DocumentDb with Mongodb protocol,你可以参考它 .