如何使用pymongo在GridFS上存储自定义元数据?

2024-04-23 15:45:01 发布

您现在位置:Python中文网/ 问答频道 /正文

最近几天,我一直在尝试添加一些自定义元数据到我插入到GridFS中的文件中。在

插入操作完全没有任何元数据,但是我尝试添加自定义元数据(使用BSON.编码(),json\u utils,作为json本身)被插入,但不正确,如下所示:

One of the incorrect isertions

(这只是众多尝试中的一个)

如果我试图从MongoVue中删除它,会出现以下错误:

Error in deleting file - Screenshot.C1.P94.74CB87859F8929FA289AAC1AF448FDE6121137C0.png
Unable to cast object of type 'MongoDB.Bson.BsonString' to type    'MongoDB.Bson.BsonDocument'.
Type: System.InvalidCastException
Stack:    at MongoDB.Bson.BsonValue.get_AsBsonDocument()
at MongoDB.Driver.GridFS.MongoGridFSFileInfo.CacheFileInfo(BsonDocument fileInfo)
at MongoDB.Driver.GridFS.MongoGridFSFileInfo.Refresh()
at MongoDB.Driver.GridFS.MongoGridFSFileInfo.get_Exists()
at MongoDB.Driver.GridFS.MongoGridFSFileInfo.Delete()

看起来我必须将元数据作为BSON文档插入,但我找不到这样做的方法。在

有什么想法吗?在

提前谢谢你!在


Tags: 文件to数据json编码getmongodbtype
1条回答
网友
1楼 · 发布于 2024-04-23 15:45:01

以下几点对我来说很好:

file_id = grid_fs.put(file_from_disk, 
  filename="feature_arrays.npz", 
  comment="compressed numpy arrays", 
  meta="this is some meta")

我认为这也可以用dict代替字符串“meta”或其他什么。您还可以使用以下关键字存储所有元数据:

^{pr2}$

据我所知,在使用python驱动程序时,您不必担心BSON。在

相关问题 更多 >