将二进制文件数据从SQL Server迁移到MongoDB GridFS

时间:2016-08-18 08:46:16

标签: c# sql-server mongodb mongodb-.net-driver

从SQL Server迁移到常规MongoDB集合后,这是我的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 https://stackoverflow.com/a/4990536/942855上找到了这个答案,它解释了如何将新文件保存到GridFS。但我需要能够将数据从SQL Server迁移到MongoGridFS。

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

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

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

1 个答案:

答案 0 :(得分:0)

这就是我最终做到的方式

连接到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导出

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对象保存在我的常规MongoCollection