- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我为我工作的公司编写了一个聊天应用程序,并且我使用 mgo 驱动程序有一段时间了。现在我们将mgo重构为官方的mongo驱动。我已经实现了 GridFS 来处理聊天文件,因为它们并不大并且简化了工作。以前的 mgo 驱动程序在保存文件时有一个数据列表,其中一个字段是 contentType(对吧?)
因此,在重构此任务中包含的大部分服务后,我注意到新的官方 mongo 驱动程序不会执行此操作??
所以我决定尝试手动添加此字段,但后来我不知道该怎么做?
尝试使用 options.GridFSUpload().SetMetadata(metadata)
但我不明白它的逻辑,互联网上关于在 GO 中工作的新 mongo 驱动程序的结果真的很少。
任何人都可以给我提示如何将自定义字段添加到文件文档中?喜欢 contentType!!
非常感谢。
这是我尝试做的一个例子
// GridFSInsert -
func GridFSInsert(fileName string, data []byte, metadata ...bsonx.Elem) (primitive.ObjectID, error) {
checkMongoConnection(false)
var fileID primitive.ObjectID
bucket, bucketErr := gridFs.NewBucket(
Mongo.Client.Database(Mongo.DBName),
options.GridFSBucket().SetName(gridFSColName),
)
if bucketErr != nil {
return fileID, bucketErr
}
uploadStream, uploadStreamErr := bucket.OpenUploadStream(
fileName,
options.GridFSUpload().SetMetadata(metadata),
)
if uploadStreamErr != nil {
return fileID, uploadStreamErr
}
defer uploadStream.Close()
fileSize, writeErr := uploadStream.Write(data)
if writeErr != nil {
return fileID, writeErr
}
fileID = uploadStream.FileID
log.Printf("Write file to DB was succesful, File size: %d", fileSize)
return fileID, nil
}
如果我遗漏了一些东西,我很抱歉,因为我对 GO 的体验并不如我所愿。
感谢您的帮助
最佳答案
没有您试图理解的逻辑。在新的官方 mongo 驱动程序中找不到太多关于 contentType 的原因是因为 contentType has been deprecated in gridfs spec早在编写驱动程序之前。
我必须承认gridfs documentation没有提到它。事实上,官方的 mongofiles cli 仍然使用旧格式。
规范直接说明了这一点:
Note: some older versions of GridFS implementations allowed applications to add arbitrary fields to the files collection document at the root level. New implementations of GridFS will not allow this, but must be prepared to handle existing files collection documents that might have additional fields.
如果你喜欢更详细的official reasoning :
Why is contentType deprecated?
Most fields in the files collection document are directly used by the driver, with the exception of: metadata, contentType and aliases. All information that is purely for use of the application should be embedded in the 'metadata' document. Users of GridFS who would like to store a contentType for use in their applications are encouraged to add a 'contentType' field to the ‘metadata’ document instead of using the deprecated top-level ‘contentType’ field.
这有点道理。驱动程序从字面上遵循规范的措辞 - 除了在 metadata
中之外,无法在任何地方创建 contentType
属性,但 Bucket.Find 仍将返回 contentType
由“旧版本”创建的文件。
从遗留 gridfs 到新格式的一次性转换可以很简单:
db.getCollection("fs.files").aggregate([
{$addFields: {
"length" : {$toLong: "$length"},
"metadata.contentType": { $ifNull: [ "$contentType", "$metadata.contentType" ] }
}},
{ $out : "fs.files" }
])
假设您的存储桶默认为“fs”并且您不打算上传旧格式的文件。如果您有足够的可用空间,out
到新的临时集合,验证它,然后重命名并不是一个糟糕的主意。
如果出于任何原因必须支持旧版格式,您仍然可以直接访问 gridfs 集合:
// in your code snippet after
fileID = uploadStream.FileID
// update the document that represent uploaded file
files := db.Collection("fs.files")
updateResult, err := files.UpdateOne(
context.Background(),
bson.D{{"_id", fileID}},
bson.D{{"$set", bson.D{{"contentType", contentType}}}},
)
其中“fs”是您的存储桶名称,contentType
是您要设置为 contentType 的字符串值。
请记住,“一些旧版本”使用 int32 作为文件长度。新驱动程序期望它是 int64。对于单独使用 *.fiiles 集合的类似查找的操作应该没问题,但可能会导致使用新的官方驱动程序下载此类文件时出现问题。
关于mongodb - Mongo-go-driver GridFS 元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54712830/
我在 mongo 中有一个查询,看起来像 db.Table_Name.group({ "key": { "Camp": true }, "initial": { "Clicks": 0 }
这是一个示例查询: db.readings.find( {"_id.s": ISODate("2012-11-01T00:05:00Z") }).count() 查询在 mongo shell 中工作
我正在使用 mongo 模板查询基于状态和邮政编码的文档,这是使用以下标准和查询方法实现的: List modelClass = null; Criteria criteria = new Crite
目前,我有一个旧版本的 mongo,即 2.6 在我的系统上运行。我的网站已经投入生产,并且拥有大量客户数据。我正在计划升级到 mongo 3.2。 所以,我的问题是 mongo v3.2 的 mon
我构建了一个 MongoDB。我想通过某些分组进行聚合。我找到了 document ,这将为我做到这一点。一切正常,但确定limitations指出: 管道的输出只能包含 16 兆字节。如果你的结果s
我无法连接到 MongoDB .在 Ubuntu 中它可以工作,但我在 CentOS 工作现在。这是错误信息: MongoDB shell version: 2.4.2 connecting to:
我试图使用mongo运行最简单的mongo-express和docker-compose容器。我遇到了许多错误,这些错误将在以后解释。 我尝试了以下docker-compose配置: 1。 versi
我有一个 mongo 查询,如下所示。 db.Course.find( { $and: [{courseCallNo: {$in : [/^ssoapicall1$/i]} }, {clientId
我想知道为什么我会收到以下 php 通知: ( ! ) Notice: Mongo::__construct(): parsing servers in C:\htdocs\multishop\lib
(问题灵感来自this one) 给定一个数据集: db.mycollection.insert([ {a:1, b:2, c:3}, {a:1, b:3, c:4}, {a:0, b:1
如果我已经使用 PECL 成功安装了 mongoDB,还需要获取 debian 软件包“php5-mongo”吗?有什么不同?(这个问题应该迁移吗?) 最佳答案 区别与从 CPAN 或 debian(
Mongo 一天前还运行良好。然后今天早上我起床并尝试打开我得到这个: MongoDB shell version: 2.6.4 2015-01-06T11:10:54.142-0500 SE
我正在尝试使用 C# Mongo 驱动程序将文件上传到 Mongo Atlas。但我不明白如何连接类 MongoServerSettings。我试过这个: private static MongoSe
我有两个版本的 mongodb,2.4.3 和 2.6.0。我可以在不同的端口上启动这两个版本,但是在使用 ./mongos 运行它时出现错误: BadValue error: no args for
我需要在 不 关闭的情况下进行分片,或者在端口 27017 中重启现有的 mongo 实例。 我尝试了以下操作,(当默认端口正在运行和 mongo 实例时) mongod --shardsvr --d
在无意中升级了 mongodb 包(3.4.9 -> 3.6.1)后,有没有办法升级 mongo 数据库? 根据 mongo 文档,作为 prerequisite在升级过程中,featureCompa
我正在使用 Sails 0.12.3 和 mongo 3.2.7 这是我的 config/connections.js。 mongo: { adapter: 'sails-mongo', host
我正在使用 mongodb 构建一个基本的搜索引擎,我已经验证了基本查询在 mongo shell 中的工作。不过,我不太明白如何将其翻译成 PHP。 输入字符串中的空格表示“和”运算符和 |或管道字
我有一个用 @Document 注释的 Mongo 集合,我希望能够从字符串 (JSON) 中获取该 Java 对象,因为我们正在将这些类作为字符串插入队列。 Spring-Data-Mongo 中是
我正在使用 Linux Debian 9。我已经安装了 JDK 1.8。我使用的maven版本是3.6,springboot的版本是2.1。 mongodb版本是3.6。 下面是我试图保存在 mong
我是一名优秀的程序员,十分优秀!