需要一个解决方案来查看网格文件是否存在,然后使用元数据更新文件。
我正在使用以下解决方案。但想要更好的一个。
不介意使用单独的Update和Delete方法。
谢谢
Public Class storedXYZ
Property Data As Stream
Property MetaData As storedXYZMetaData
End Class
Public Sub SaveStoredXYZ(storedXYZ As StoredXYZ)
Dim MongoGridFSCreateOptions As New MongoDB.Driver.GridFS.MongoGridFSCreateOptions
Dim qry As IMongoQuery
qry = Query.EQ("metadata.StoredXYZId", BsonValue.Create(storedXYZ.MetaData.StoredXYZ.ToString()))
Dim gridFile As MongoGridFSFileInfo = mdbGridFS.FindOne(qry)
If gridFile IsNot Nothing Then
Dim mongoStream As MongoGridFSStream
MongoGridFSCreateOptions.Metadata = storedXYZ.MetaData.ToBsonDocument
mongoStream = gridFile.OpenWrite()
''Convert MongoStream to MemoryStream
Dim fs As Stream = New MemoryStream()
Dim buffer As Byte() = New Byte(9999) {}
Dim bytesRead As Integer = 0
Do
bytesRead = storedXYZ.Data.Read(buffer, 0, buffer.Length)
mongoStream.Write(buffer, 0, bytesRead)
Loop While bytesRead > 0
mongoStream.Seek(0, SeekOrigin.Begin)
mongoStream.Position = 0
mdbGridFS.SetMetadata(gridFile, storedAXYZ.MetaData.ToBsonDocument)
Else
MongoGridFSCreateOptions.Metadata = storedXYZ.MetaData.ToBsonDocument
Dim fileinfo As MongoGridFSFileInfo
fileinfo = mdbGridFS.Upload(storedXYZ.Data, storedXYZ.MetaData.Name, MongoGridFSCreateOptions)
End If
End Sub
答案 0 :(得分:1)
而不是试图覆盖当前文件,为什么不只是触发删除命令,然后重新上传文件并将文档更新为旧文件?
答案 1 :(得分:0)
当您使用现有文件名上传文件时,MongoGridFS上传方法会获取该文件的新版本。
将OpenWrite与现有文件名一起使用可以更新文件。 C#驱动程序是唯一允许更新GridFS文件的驱动程序。
答案 2 :(得分:0)
您可以将fs.files和fs.chnuks集合用作任何其他集合,但您必须小心保持GridFS数据的重要性不受影响。