折腾:
【已解决】把本地mp3文件存入在线Mongo中且填写meta信息
后,需要:
想办法去更新之前保存到Mongo的gridfs中的mp3文件的metadata信息,
同时希望保持原先file的id不变。
之所以有这个要求是因为之前听说:mongo中保存的文件,不支持直接更新
如果是这样,那么每次修改一个文件,内部文件id就变化了,那么:
之前其他系统中已经保存的mongo的file的id,就失效了。
所以才会要求更新文件信息,保证之前file的id不变。
mongo update gridfs file id not change
[CSHARP-2056] How to update GridFS metadata after the file has been uploaded – MongoDB
ruby – MongoDB GridFS replace/update metadata – Stack Overflow
java – How to overwrite image in mongoDB gridfs? – Stack Overflow
python – Updating metadata for gridfs file object – Stack Overflow
mongo 更新 id 不变
mongo 更新 gridfs id 不变
mongo 更新 gridfs id会变化吗
mongo gridfs 更新 file
mongo gridfs update file
mongodb – How to perform Update operations in GridFS (using Java)? – Stack Overflow
感觉或许可以:
可以把新的文件的内容和metadata,写入mongo的gridfs,同时指定id为之前的文件的id
How to update saved document in GridFS – Google Groups
“Do not use GridFS if you need to update the content of the entire file atomically. As an alternative you can store multiple versions of each file and specify the current version of the file in the metadata. You can update the metadata field that indicates “latest” status in an atomic update after uploading the new version of the file, and later remove previous versions if needed.”
即:不建议直接更新整个gridfs中的file
而是建议 再存入新版本的file,在meatada中保存version字段,表示版本
之后根据情况去获取version是latest的版本的文件
[mongodb-user] How to perform Update operations in GridFS (using Java)? – Grokbase
此处,再去看看:
除了update可以更新metadata之外,是否可以方便的更新file的binary data
中倒是有:
“data” : <binary>
不过感觉不方便直接去修改每个chunk的data
mongo gridfs update python
gridfs – Tools for working with GridFS — PyMongo 3.7.1 documentation
“put(data, **kwargs)
Put data in GridFS as a new file.
Equivalent to doing:
<code>try: f = new_file(**kwargs) f.write(data) finally: f.close() </code>
data can be either an instance of str (bytes in python 3) or a file-like object providing a read() method. If an encoding keyword argument is passed, data can also be a unicode (strin python 3) instance, which will be encoded as encoding before being written. Any keyword arguments will be passed through to the created file – see GridIn() for possible arguments. Returns the “_id” of the created file.
If the “_id” of the file is manually specified, it must not already exist in GridFS. OtherwiseFileExists is raised.”
看起来是可以通过:
put创建一个新的gridfs,同时指定id的
只是自己确保,在添加新file之前,把旧file删除掉,记住旧id,然后传入新file的id
而传入其他的kwargs参数有哪些,可以参考:
grid_file – Tools for representing files stored in GridFS — PyMongo 3.7.1 documentation
“* “_id”: unique ID for this file (default: ObjectId) – this “_id” must not have already been used for another file”
记住:传入 _id 时,应该是: ObjectId(“xxx”),而不是 “xxx”
GridFS Example — PyMongo 3.7.1 documentation
这样就清楚了,抽空去写代码更新即可。
总体思路是:
使用python的GridFS的库
先找到要更新的gridfs的file
旧file的信息先暂存着待后续使用
删除旧file
用put去写入新file,传入:
_id = ObjectId(“旧file的id”)
及其他参数:
filename = 新的mp3的文件名
contentType = 新的mp3的值:audio/mpeg
metadata = 新的metadata值
即可。
然后调试期间,确保put新建文件的返回的id是之前旧的id
TODO:
继续参考前面几个帖子
grid_file – Tools for representing files stored in GridFS — PyMongo 3.7.1 documentation
GridFS Example — PyMongo 3.7.1 documentation
gridfs – Tools for working with GridFS — PyMongo 3.7.1 documentation
去写代码
不过,也可以去看看MongoDB官网对于gridfs的操作,是否有update的接口:
“Do not use GridFS if you need to update the content of the entire file atomically. As an alternative you can store multiple versions of each file and specify the current version of the file in the metadata. You can update the metadata field that indicates “latest” status in an atomic update after uploading the new version of the file, and later remove previous versions if needed.”
官方的意思也是:想要更新信息,就存入(更新)版本的文件,其中用metadata中保存version版本信息,然后再删除之前的版本。
-》所以就是没有update接口。