首页 文章

使用Azure DocumentDB的MongoDB Indexes.CreateOneAsync异常

提问于
浏览
1

我正在尝试使用连接到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 回答

  • 0

    this blog,我们发现它目前不支持 Unique indexes .

    一般可用性只是我们为DocumentDB存储的所有功能和改进的开始:MongoDB的API . 在不久的将来,我们将发布对Unique索引的支持以及几项主要的性能改进 .

    这个帖子讨论了Cannot create index in Azure DocumentDb with Mongodb protocol,你可以参考它 .

相关问题