Domanda

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
    }
]

}

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.

I know that the mongo query should be something like this :

db.collection.find("tags":{
            $all:[
                {"$elemMatch":{"name":"women","type":3}},               
            ]})

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?

È stato utile?

Soluzione

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top