简体   繁体   中英

Spring Mongo criteria on list of list

Hi I am using spring data mongo, I need to fetch data based on multiple where condition. The problem I have when I want to apply a where clause to a list in a list.

For example

{
    "_id" : ObjectId("5982bf9339f3c92b84be4737"),
    "_class" : "com.paladion.payment.model.GroupQuestionMapping",
    "saqID" : "SAQ A",
    "saqVersion" : "3",
    "questionTab" : {
        "Secure Network" : [ 
            {
                "number" : "2.1 (a)",
                "question" : "Are vendor-supplied"
        "description" : "<ul><li>Review"
        }, 
            {
                "number" : "2.1 (b)",
                "question" : "Are unnecessary"
        "description" : "<ul><li>Review policies 
        }
         ],
        "Access Control" : [ 
            {
                "number" : "2.1 (a)",
                "question" : "Are vendor-supplied"
        "description" : "<ul><li>Review"
        }, 
            {
                "number" : "2.1 (b)",
                "question" : "Are unnecessary"
        "description" : "<ul><li>Review policies 
        }
        ]
    }
}

Over here I need to fetch data where saqId is SAQ A and saq Version is 3 and questionTab is secure network.

I have problem in applying criteria on questionTab.

my code:

Query query = new Query();
query.addCriteria(Criteria.where("saqtype").is(saqType));
query.addCriteria(Criteria.where("saqversion").is(saqVersion));
query.addCriteria(/* criteria on questionTab */);
query.addCriteria(Criteria.where("questionTab.Secure Network").exists(true));

Thing to note is that it will bring you the full document based on criteria, so you would have to filter out other type of questionTab from document.

Other way is aggregation but then I think processing on application layer might be preferable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM