mongodb - 从 gridFS 获取数据时的性能循环

标签 mongodb pymongo gridfs

我正在使用 pymongo 从 gridFS 获取数据,获取这些数据时的循环非常慢。

是否有可能避免该循环,或者有什么方法可以更快地做到这一点??

from pymongo import MongoClient
from pprint import pprint
import bson
from gridfs import GridFS
import json
import pandas as pd

client = MongoClient()

client.database_names()
db = client['MC']

fs = GridFS(db, collection="MC")

db.collection_names(include_system_collections=False)
collectionFiles = db['MC.files']
collectionChunk = db['MC.chunks']

files = db['MC.files'].find({"metadata.Feature0": "00011"})

for n in files:
    file_id = n['_id']
    chunks = db['MotorCalculo.chunks'].find({"files_id": file_id})
    bsondData = (fs.get(file_id).read())
    decData = bsondData.decode()
    jsonData = json.loads(decData)
    F1 = jsonData['Feature1']
    F2 = jsonData['Feature2']

最佳答案

如果您有足够的 RAM,访问文件组应该会更快并且不会对 mongo 进行多次调用。

你可以尝试这样的事情:

batch_file_id = ['#1', '#2', '#3', '#4']
chunks = db['MotorCalculo.chunks'].find('{\"files_id\" : {\"$in\":[{\"$oid\":\"' + '\"}, {\"$oid\":\"'.join(batch_file_id) + '\"}]}}')

...

batch_file_id
Out[1]: ['#1', '#2', '#3', '#4']

'{\"files_id\" : {\"$in\":[{\"$oid\":\"' + '\"}, {\"$oid\":\"'.join(batch_file_id) + '\"}]}}'
Out[2]: '{"files_id" : {"$in":[{"$oid":"#1"}, {"$oid":"#2"}, {"$oid":"#3"}, {"$oid":"#4"}]}}'

问候!!

关于mongodb - 从 gridFS 获取数据时的性能循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42851809/

相关文章:

mongodb - 如何指定 GridFS 存储桶?

javascript - 如何打印 numberlong 值?

mongodb - mongochef azure 文档数据库查询问题

java - 如何覆盖 mongoDB gridfs 中的图像?

mongodb - 上传后使用 ExpressJS 将文件存储在 Mongo 的 GridFS 中

python - 如何使用 PyMongo find() 搜索嵌套数组属性?

javascript - 处理 meteor 中的新记录

node.js - 如何在查询中使用 $push 将数据插入子文档,而不是检索文档并将其保存回来

python - 如何使用python从bson日期/时间对象或时间戳获取isodate字符串?

mongodb,pymongo,聚合给出奇怪的输出(关于游标的东西)