import pymongo
import gridfs
import bson
sourceDBConnection = pymongo.Connection(host='192.168.1.102',port=27017,slave_okay=True)['kingdeeMongo']
destinationDBConnection = pymongo.Connection(host="localhost",port=27017,slave_okay=True)['test']
sourceFS = gridfs.GridFS(sourceDBConnection)
destinationFS = gridfs.GridFS(destinationDBConnection)
#情况一:把fs.files中的所有信息都保存过去,fs.files中文档的字典信息不变,同时自动添加到fs.chunks
recordone = sourceDBConnection["fs.files"].find_one({
{"_id": bson.objectId.ObjectId('53eada8e5a1e0a1dbc7fbcd1')}})
sourceFile = sourceFS.get(recordone['_id'])
destinationFS.put(sourceFile.read(),**recordone)
#情况二:只是把fs.files中的所标志的对象信息(如:图片或视频)保存过去,其他的对该对象的说明信息(_id)重新由系统定义
recordtwo = sourceDBConnection["fs.files"].find_one()
sourceFiletwo = sourceFS.get(recordtwo['_id'])
de
在Python中如何将mongodb库中的fs.file中的内容迁移到另一mongodb中去问题
最新推荐文章于 2022-07-19 10:55:30 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)