将二进制文件数据从 SQL 服务器迁移到 MongoDB GridFS

Migrate binary files data from SQL Server to MongoDB GridFS

这是我从 SQL 服务器迁移到常规 Mongo 数据库集合后的 Mongo 文档。

{
"TicketId": 23,
"Attachments" : [ 
        {
            "_id" : 4221,
            "Name" : "profile Pic",
            "Size" : 218112,
            "Description" : "User Profile Pic",
            "Data" :{ "$binary" : "0M8R4KGxGuE.............",
            "IsPrivate" : false,
            "AttachmentType" = {
                                 "ContentType"   = "image/png",
                                 "FileExtension" = ".png"
                               },
            "CreatedByUserId" : 12,
            "CreatedDateTimeUtc" : ISODate("2012-05-21T18:40:08.570Z"),
        },
     { // Another attachment },
     { // Another attachment },
     { // Another attachment }]
}

但是我有超过 16 MB 的附件,因为 MongoDB 文档大小限制为 16 MB 我不能使用这种方法来保存我的附件。

看起来 GridFS 是在 MongoDB 中保存文件的正确方法 我在 SO 上找到了这个答案,它解释了如何将新文件保存到 GridFS。但我需要能够将数据从 SQL 服务器迁移到 MongoGridFS。

此外,当您将文件上传到 GRIDFS 时,似乎生成的默认字段很少,我想知道如何向其添加其他字段以映射到其他集合?

或者我应该考虑将所有与信息相关的附件与其他映射集合一起保存,并向其中添加 gridFsInfo.Id 的数组以进行映射?

我正在使用 MongoDB 3.2 和 MongoDB C# 驱动程序

这就是我最终的做法

连接到 MongoGridFs

  MongoCredential mongoCredential = MongoCredential.CreateCredential("dbName", "userName", "password");
            var mongoServerSettings = new MongoServerSettings {Server = new MongoServerAddress("host Ip",27017),
                Credentials = new List<MongoCredential> { mongoCredential },
                ConnectionMode = ConnectionMode.Automatic, ConnectTimeout = new TimeSpan(0,0,0,30)};
            var mongoServer = new MongoServer(mongoServerSettings);
            var mongoGridFsSettings = new MongoGridFSSettings { };
            var MongoGridFs = new MongoGridFS(mongoServer, DatabaseName, mongoGridFsSettings);

在我的 C# SQL-to-Mongo export

ticket.Attachments = (from ta in context.TicketAttachments
                                          join a in context.Attachments on ta.AttachmentId equals a.Id
                                          join at in context.AttachmentTypes on a.TypeId equals at.Id
                                          where ta.TicketId == ticket.Id
                                          select new Domain.Model.Tickets.Attachment
                                          {
                                              Id = a.Id,
                                              // Load all fields
                                              Data = a.Data,
                                              Size = a.Size,
                                              Name = a.Name,
                                              AttachmentType = new Domain.Model.Tickets.AttachmentType()
                                              {
                                                  ContentType = at.ContentType,
                                                  FileExtension = at.FileExtension
                                              }
                                          }).ToList();

                    foreach (var attachment in ticket.Attachments)
                    {
                        Stream stream = new MemoryStream(attachment.Data);
                        MongoGridFSFileInfo mongoGridFsFileInfo = mongoDbContext.MongoGridFs.Upload(stream, attachment.Name);
                        attachment.GridFsObjectId = mongoGridFsFileInfo.Id.AsObjectId;
                    }

最终将我的 ticket.Attachments 对象保存在我的常规 Mongo 集合