{ _id: 1, results: [ "tokyo", "japan" ] }
{ _id: 2, results: [ "sydney", "australia" ] }
db.scores.find(
{ results: { $elemMatch: { $regex: *some regex* } } }
)
如何使用spring mongodb数据查询标准转换这个简单的elemMatch示例?
如果数组包含对象,我可以这样做
Criteria criteria =
Criteria.where("results").
elemMatch(
Criteria.where("field").is("tokyo")
);
但在我的问题中,我没有“领域”
更新:
我认为Veeram的答案在尝试之后会起作用
Criteria criteria =
Criteria.where("results").
elemMatch(
new Criteria().is("tokyo")
);
它没有返回任何东西。我错过了什么吗?
当我检查查询对象时,它声明以下内容:
Query: { "setOfKeys" : { "$elemMatch" : { }}}, Fields: null, Sort: null
另一方面,如果我使用如上所述的Criteria.where(“field”)修改标准,
Query: { "setOfKeys" : { "$elemMatch" : { "field" : "tokyo"}}}, Fields: null, Sort: null
我得到了一些东西,但这不是我的数据结构,结果是一个字符串数组而不是对象。我实际上需要使用正则表达式,为简单起见,上面的例子是使用.is
您可以尝试以下查询。
Criteria criteria = Criteria.where("results").elemMatch(new Criteria().gte(80).lt(85));