我已阅读mongo手册3.6。它有一个例子json - bios ExampleCollection,如下所示:
final Window mRootWindow = getWindow();
ll_main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
public void onGlobalLayout(){
int screenHeight = ll_main.getRootView().getHeight();
Rect r = new Rect();
View view = mRootWindow.getDecorView();
view.getWindowVisibleDisplayFrame(r);
int keyBoardHeight = screenHeight - r.bottom;
}
});
根据手册,要查询文档数组,QL是这样的:
{
"_id" : 1,
"name" : {
"first" : "John",
"last" : "Backus"
},
"birth" : ISODate("1924-12-03T05:00:00Z"),
"death" : ISODate("2007-03-17T04:00:00Z"),
"contribs" : [
"Fortran",
"ALGOL",
"Backus-Naur Form",
"FP"
],
"awards" : [
{
"award" : "W.W. McDowell Award",
"year" : 1967,
"by" : "IEEE Computer Society"
},
{
"award" : "National Medal of Science",
"year" : 1975,
"by" : "National Science Foundation"
},
{
"award" : "Turing Award",
"year" : 1977,
"by" : "ACM"
},
{
"award" : "Draper Prize",
"year" : 1993,
"by" : "National Academy of Engineering"
}
]
}
我使用spring mongo模板使用上面的表达式查询此文档:
db.bios.find(
{
awards: {
$elemMatch: {
award: "Turing Award",
year: { $gt: 1980 }
}
}
}
)
我不明白为什么结果列表为空。我调试最后一个语句并观察查询变量。它的字符串格式是:
Query query = new Query();
query.addCriteria( Criteria.where("awards").elemMatch(
Criteria.where("award").is("Turing Award").and("year").gt(new Integer(1980))) );
List<TestEntity> list = mongoTemplate.find(query, TestEntity.class, "testentity");
Assert.assertTrue(!list.isEmpty());
//testentity is a collection in my local db to store example json text.
并复制
Query: { "awards" : { "$elemMatch" : { "award" : "Turing Award" , "year" : { "$gt" : 1980}}}}, Fields: null, Sort: null
到robo 3T mongo客户端工具查询。它查询3个结果。 java代码如何工作?
谢谢。
答案 0 :(得分:0)
尝试更改
{
"award" : "Turing Award",
"year" : 1977,
"by" : "ACM"
},
到
{
"award" : "Turing Award",
"year" : 1981,
"by" : "ACM"
}