使用mongo-java-driver在Matlab中进行高级mongoDB查询

时间:2015-05-27 11:34:43

标签: java matlab mongodb

我正在尝试使用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编写的。

感谢。

3 个答案:

答案 0 :(得分:1)

ForecastEntries_Coll = db.getCollection('ForecastEntries');

ID = '553e24c1a46da1d1498b4567';

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

fcstResources = ForecastEntries_Coll.find(query);

答案 1 :(得分:0)

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

直接来自the Matab documentation,其中的某些内容应该可以导入ObjectId

import org.bson.types.ObjectId

答案 2 :(得分:0)

昨天我尝试导入' org.bson.types.ObjectId'正如sylvain-leroux所建议的那样,但它并没有奏效。我认为这不是一个简单的解决方案,因为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查询可能是不可能的: - (