【发布时间】: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'。”