使用mongodb中的spring数据根据搜索条件从数组和子数组中筛选出多个值

时间:2019-03-18 06:28:44

标签: mongodb spring-data

我想基于多个匹配条件从集合中检索所有数据。我的收藏结构如下所示

{
        "general": {
            "filename": "GH010070.MP4",
            "duration": "00:06:50.155",
            "encoded_time": "UTC 2019-01-28 09:13:11",,
            "patoroller": "Mark"
        },
        "frames": [
            {
                "frame_number": 59,
                "filename": "000000984.jpg"

                "detect_objects": [
                    {
                        "class": "person",
                        "confidence": 0.9639755487442016,
                        "rect": {
                            "height": 331
                        },
                        "attributes": [
                            {
                                "category": "age",
                                "classifier": "MC",
                                "results": [
                                    {
                                        "classname": "Larger60",
                                        "confidence": 0.9
                                    }
                                ]
                            },
                            {
                                "category": "footwearstocking",
                                "classifier": "BC",
                                "results": [
                                    {
                                        "classname": "Stocking",
                                        "confidence": 0.5634149151683278
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "frame_number": 119,
                "filename": "000001985.jpg"
                "detect_objects": [
                    {
                        "class": "car",
                        "confidence": 0.8223795890808106,
                        "rect": {
                            "height": 29
                        },
                        "attributes": [
                            {
                                "category": "car model",
                                "classifier": "MC",
                                "results": [
                                    {
                                        "classname": "JaguarXKXKR2012",
                                        "confidence": 0.9
                                    },
                                    {
                                        "classname": "BMW3SeriesWagon2012",
                                        "confidence": 0.022097986763236066
                                    },
                                    {
                                        "classname": "ChevroletCorvetteZR12012",
                                        "confidence": 0.013909696827658407
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
            //....
        ]
    }

“我已尝试使用spring数据api来获取所有汽车:”

    Criteria elementMatchCriteria = Criteria.where("frames.detect_objects").elemMatch(Criteria.where("class").is("car"));
    Query query = Query.query(elementMatchCriteria);
    query.fields().position("frames.detect_objects", 8);
    VideoDetails videoDetails = mongoOperations.findOne(query, VideoDetails.class);
    return videoDetails;

即使在原始数组(框架)中,也存在多辆汽车,但它仅返回第一个匹配的对象。请建议我,如何解决此问题。

谢谢。

0 个答案:

没有答案