【问题标题】:MongoDB Indexes.CreateOneAsync Exception using Azure DocumentDB使用 Azure DocumentDB 的 MongoDB Indexes.CreateOneAsync 异常
【发布时间】: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


【解决方案1】:

this blog,我们可以发现它目前不支持唯一索引

公开发布只是我们为 DocumentDB 存储的所有功能和改进的开始:API for MongoDB。在不久的将来,我们将发布对唯一索引的支持和几项重大的性能改进。

本帖讨论Cannot create index in Azure DocumentDb with Mongodb protocol,你可以参考一下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2018-09-21
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多