2013-05-13 19 views
6

我的文档结构如下:

{ 
"scores": [{ 
    "scoreTitle": "environment", 
    "scoreValue": 3, 
    "scoreDescribe": "good" 
}, { 
    "scoreTitle": "service", 
    "scoreValue": 3, 
    "scoreDescribe": "good" 
}, { 
    "scoreTitle": "taste", 
    "scoreValue": 4, 
    "scoreDescribe": "good" 
}] 
} 

在蒙戈外壳,我可以使用下面的查询来发现其中有一个分数,其文档标题是“环境”和值大于2

db.reviews.find({"scores":{"$elemMatch":{"scoreValue":{"$gt":2},"scoreTitle":"environment"}}}) 

现在我想查询使用吗啡的文档,从API文档中,“ELEM”运营商在fiter方法的支持,以及其他查询标准对象是必需的,例如,查询具有标题分数的文档是“环境”,并描述为“好”:

Score score = new Score();// the criteria object, only support comparisons of equality 
score.setScoreTitle("environment"); // title should be environment 
score.setScoreDescribe("good"); // describe should be good 
Query q = ds.createQuery(Review.class).filter("scores elem", score); 

我的问题是:如何让我在查询条件的数字比较,也就是说,我怎样才能使具有相同的效果查询作为mongo shell的副本。

回答

1

我想你可以尝试

ds.createQuery(Review.class). 
    filter("scores.scoreValue >=", 2). 
    filter("scores.scoreTitle", "environment"). 
    retrievedFields(true, "scores.$")