【问题标题】:Advanced mongoDB query in Matlab using mongo-java-driver使用 mongo-java-driver 在 Matlab 中进行高级 mongoDB 查询
【发布时间】:2015-05-27 11:34:42
【问题描述】:

我正在尝试使用 mongo-java-driver-2.13.1.jar 从 matlab 执行 mongoDB 查询。我对 mongodb 不熟悉,但我有一个查询可以很好地与 robomongo 配合使用。查询是:

db.getCollection('ForecastPrice').find({'forecast': ObjectId('553e24c1a46da1d1498b4567')})

在 matlab 中,我能够连接到远程数据库、浏览和检索集合。以下是我从 Matlab 连接的步骤:

clear; clc

javaaddpath('/home/maurice/MATLAB/myJavaClasses/mongo-java-driver-2.13.1.jar')

import com.mongodb.*

% Connect to remote server
m = Mongo('5.xxx.105.xxx',27017); % 

% open DB
db = m.getDB('database');

% Authentication
db.authenticate('User','WCaAjaks');

% See collection:
colls = db.getCollectionNames(); % get collection name
% Read a collection:
clients = db.getCollection('Clients')

% Read data from a collection
fcstPrices = db.getCollection('ForecastPrice').find(); % OK!!!

但如果我尝试按 ObjectId 进行过滤,我会收到以下错误:

>> fcstPrices = db.getCollection('ForecastPrice').find({'forecast': ObjectId('553e24c1a46da1d1498b4567')})
Undefined function 'ObjectId' for input arguments of type 'char'.

我可以循环这个集合的值,比较 ID 并保留那些我感兴趣的,但这在访问大集合时既不优雅也不快速:

size_fcstRes = fcstPrices.size;
nombre_fcstRes = fcstPrices.count %% Es el mateix que size?

output = {};
for i=1:size_fcstRes

    if i==1
        valors = char(fcstPrices.one);
    else
        valors = char(fcstPrices.next);
    end

    strc_valors = loadjson(valors);

    try

    if strcmp(id_prod, strc_valors.content.products.id)                    % Check if it is the ID we are looking for

        if strcmp(nomVar, strc_valors.content.products.variables.name)     % Check if it is the Variable we are looking for

            display(num2str(i))

            start_date = datenum((strc_valors.content.date), 'yyyymmddHHMMSS' );                         % Data del primer valor
            values = strc_valors.content.products.variables.values;        % Valors

            output{i,1} = cat(2, start_date, values);

        end

    end

    catch error_mongodb

        display(['Error: ', error_mongodb.message])

    end
end

如果有人能帮我解决这个问题,我将不胜感激。我必须从 mongoDB 获取一些数据,我想从 Matlab 中获取,因为我的脚本完全是用 Matlab 编写的。

谢谢。

【问题讨论】:

  • 关于错误,你试过没有 ObjectId ,只是字符串?
  • 我尝试了很多组合,但没有一个有效。输出始终相同:“没有为类 'com.mongodb.DBCollectionImpl' 找到具有匹配签名的方法 'find'。”

标签: java matlab mongodb


【解决方案1】:
ForecastEntries_Coll = db.getCollection('ForecastEntries');

ID = '553e24c1a46da1d1498b4567';

query = BasicDBObject('forecast', ObjectId(ID));

fcstResources = ForecastEntries_Coll.find(query);

【讨论】:

    【解决方案2】:

    您需要额外导入 ObjectId belongs to org.bson.types


    直接来自the Matab documentation,类似的东西应该可以导入ObjectId

    import org.bson.types.ObjectId
    

    【讨论】:

    • 感谢您的 cmets Sylvain。如何将此类/包导入 Matlab?
    • @jordividal Glad to have helped you. 我不是 Matlab 专家,但根据我阅读的here,您只需要一个额外的导入语句。查看我的编辑。
    【解决方案3】:

    昨天我尝试按照sylvain-leroux 的建议导入“org.bson.types.ObjectId”,但没有成功。我认为这不是一个简单的解决方案,因为 mongo-java-driver 在行中寻找 " 而 Matlab 不允许它们:

    fcstResources = db.getCollection('ForecastEntries').find({"forecast": ObjectId("553e24c1a46da1d1498b4567")}); % KO
                                                               |
    Error: The input character is not valid in MATLAB statements or expressions.
    

    这个“输入字符”指的是“。另一方面,如果我尝试使用 Matlab sintaxis,它也不起作用:

    fcstResources = db.getCollection('ForecastEntries').find({'forecast': ObjectId('553e24c1a46da1d1498b4567')}); % KO
    Undefined function 'colon' for input arguments of type 'org.bson.types.ObjectId'.
    

    看起来我陷入了死胡同......从Matlab进行这种mongodb查询可能是不可能的:-(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 2019-10-14
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多