Morphia-基于子文档的查询 [英] Morphia - Query based on a subdocument
问题描述
我有一个mongo集合,其中包含如下文件:
I have a mongo collection which has documents which look like below :
{
"_id" : ObjectId("9873214jkhdkfjdsf8324"),
"nm" : "test",
"sts" : 1,
"updby" : NumberLong(0),
"tags" : [
{
"name" : "women",
"rank" : 1,
"type" : 3
},
{
"name" : "men",
"rank" : 1
},
{
"name" : "clothing",
"rank" : 2,
"type" : 1
}
]
}
我要查询集合,以便要返回的每个文档的标签子文档中所有具有名称":"women"和"type":3的文档.
I want to query the collection such that I want all the documents which have "name": "women" and "type" : 3 inside the tags subdocument of each document which is returned.
我知道mongo查询应该是这样的:
I know that the mongo query should be something like this :
db.collection.find("tags":{
$all:[
{"$elemMatch":{"name":"women","type":3}},
]})
我尝试使用了morphia提供的'hasthiselement',但是我无法形成我想要的确切查询.
I tried using the 'hasthiselement' provided by morphia, but I am not able to form the exact query which I want.
getMongoDAORead().getDatastore().createQuery(test.class)
.field("tags").hasThisElement("name").equal("women");
此查询似乎不正确.有人可以帮助我形成正确的查询吗?
This query doesn't seem to be correct. Can someone help me form the correct query?
推荐答案
我通过以下操作解决了此问题:
I fixed this by doing the following:
我创建了标签类的对象并对其进行了初始化:
I created a object of the Tags Class and initialized it:
Tags tag = new Tags("women", null, 3);
Query<MyClass> t = getMongoDAORead().getDatastore()
.createQuery(MyClass.class)
.field("ctags").hasThisElement(tag);
这篇关于Morphia-基于子文档的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!