【问题标题】:Command createIndexes failed: 'unique' is not supported on MongoDB.Driver.GridFS.UploadFromStream命令 createIndexes 失败:MongoDB.Driver.GridFS.UploadFromStream 不支持“唯一”
【发布时间】:2017-10-25 13:38:30
【问题描述】:

我正在使用 C# MongoDB.Driver,版本=2.4.4。

错误信息:

不支持“唯一”

代码片段:

using (FileStream file = new FileStream(@"C:\Users\aagrawal\Desktop\Amit.png", FileMode.OpenOrCreate))
            {
                var bucket = new GridFSBucket(mongoDB, new GridFSBucketOptions
                {
                    BucketName = "excelFiles",
                    ChunkSizeBytes = 1048576, // 1MB
                    WriteConcern = WriteConcern.WMajority,
                    ReadPreference = ReadPreference.Secondary
                });

                var id = bucket.UploadFromStream(fileName, file);
            }

完整的堆栈跟踪:

在 MongoDB.Driver.Core.WireProtocol.CommandWireProtocol1.ProcessReply(ConnectionId connectionId, ReplyMessage1 回复)在 MongoDB.Driver.Core.WireProtocol.CommandWireProtocol1.Execute(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocol[TResult](IWireProtocol1 协议,CancellationToken 取消令牌)在 MongoDB.Driver.Core.Servers.Server.ServerChannel.Command[TResult](数据库命名空间 databaseNamespace、BsonDocument 命令、IElementNameValidator commandValidator, Func1 responseHandling, Boolean slaveOk, IBsonSerializer1 resultSerializer, MessageEncoderSettings messageEncoderSettings, CancellationToken cancelToken) 在 MongoDB.Driver.Core.Operations.CommandOperationBase1.ExecuteProtocol(IChannelHandle channel, ServerDescription serverDescription, ReadPreference readPreference, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.CommandOperationBase1.ExecuteProtocol(IChannelSource channelSource, ReadPreference readPreference, CancellationToken 取消令牌)在 MongoDB.Driver.Core.Operations.WriteCommandOperation1.Execute(IWriteBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.CreateIndexesUsingCommandOperation.Execute(IWriteBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.CreateIndexesOperation.Execute(IWriteBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.GridFS.GridFSBucket1.EnsureIndexes(IReadWriteBindingHandle 绑定,CancellationToken 取消令牌)在 MongoDB.Driver.GridFS.GridFSBucket1.OpenUploadStream(TFileId id, String filename, GridFSUploadOptions options, CancellationToken cancellationToken) at MongoDB.Driver.GridFS.GridFSBucket1.UploadFromStream(TFileId id, 字符串文件名、流源、GridFSUploadOptions 选项、 CancellationToken 取消令牌)在 MongoDB.Driver.GridFS.GridFSBucket.UploadFromStream(字符串文件名, 流源、GridFSUploadOptions 选项、CancellationToken cancelToken) 在 CRUDOperationAzureMongo.Program.Main(String[] args) 在 C:\Users\aagrawal\documents\visual studio 2017\Projects\CRUDOperationAzureMongo\CRUDOperationAzureMongo\Program.cs:line 110

【问题讨论】:

  • 你在使用分片集合吗?
  • 首先,我使用的是 Azure CosmosDB,它在后面的代码中使用了 MongoDB 3.2.0。
  • 是的,在这种情况下,我使用的是共享集合。

标签: c# mongodb azure-cosmosdb


【解决方案1】:

C# 驱动程序检查您的块集合上的{ "files_id": 1, "n": 1 } 上是否有索引。如果没有,它将尝试使用此规范创建一个唯一的。

在分片集合的情况下,这可能会出错。你可以阅读这个here。如果您使用的是分片集合,请确保您遵循文档的this 部分。

您也可以尝试使用上述规范手动创建非唯一索引,理论上应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2013-10-26
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    相关资源
    最近更新 更多