这是我从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#驱动程序一起使用
这就是我最终做的方式
连接到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到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中
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句