嗨我对mongodb查询api比较新,我使用mongodb java driver v 3.2。
有人可以举个例子来提取文件,其中包括" prefContributors" =" RMDSContri2"和" dayString"是在一系列日期之间。? 我在下面提供了我的努力
{_id: "INSTR_ID9:RMDSContri1:2015-12-29 18:01:54.589"
priceQuote:
{
instrId: "INSTR_ID9"
dayString: "Dec 29, 2015 11:31:54 PM"
prefContributors: "RMDSContri1"
askPrice: 339.13794
bidPrice: 333.13794
midPrice: 336.13794
}
}
{_id: "INSTR_ID9:RMDSContri2:2015-12-29 18:01:56.589"
priceQuote:
{
instrId: "INSTR_ID9"
dayString: "Dec 29, 2015 11:31:56 PM"
prefContributors: "RMDSContri2"
askPrice: 374.5009
bidPrice: 368.5009
midPrice: 371.5009
}
}
以下带浮点的代码:
BasicDBObject query = new BasicDBObject("priceQuote.prefContributors", "ReutersContri3");
query.append("priceQuote.askPrice", new BasicDBObject().append("$gte", 100.00f).append("$lte", 101.00f));
使用" UTC"存储和检索日期字符。时区。
但是,以下带日期的代码不会返回任何结果。
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date start = format.parse("2015-12-29 18:00:00");
Date end = format.parse("2015-12-29 19:00:00");
BasicDBObject query = new BasicDBObject("priceQuote.prefContributors", "ReutersContri2");
query.append("priceQuote.dayString", new BasicDBObject().append("$gte", start).append("$lte", end));
还尝试使用BasicDBObjectBuilder,但仍未返回任何结果。
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date start = format.parse("2015-12-29 18:00:00");
Date end = format.parse("2015-12-29 19:00:00");
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start("$gte", start).add("$lte", end);
BasicDBObject query = new BasicDBObject("priceQuote.prefContributors", "ReutersContri3");
query.append("priceQuote.askPrice", builder.get());