如何使用 Java 从 MongoDB 读取嵌套数组

How to read nested array from MonogDB using Java

我需要在 eventDetails.eventDelivery.stateCode = "Y-FINISH" 中找到 record/count。它可以在数组列表中的任何元素中。

只想找到数组列表中 stateCode = "Y-FINISH" 的位置。

Sample Data #1:
{
    "_id" : ObjectId("5a2fb09736cf07f6a1146691"),
    "activityID" : "",
    "eventDetails" : [ 
        {
            "eventDelivery" : {
            "digital" : {
                "secureid" : "1231321212"
            },
            "stateCode" : "X-FINISH",
            "state" : "SUCCESS"
        }, 
        {
            "eventDelivery" : {
            "digital" : {
                "secureid" : "8762871121"
            },
            "stateCode" : "Y-FINISH",
            "state" : "SUCCESS"            
        }
    ],
}

Sample Data #2:
    {
        "_id" : ObjectId("5a2fb09736cf07f6a1146691"),
        "activityID" : "",
        "eventDetails" : [ 
            {
                "eventDelivery" : {
                "digital" : {
                    "secureid" : "1231321212"
                },
                "stateCode" : "X-FINISH",
                "state" : "SUCCESS"
            }, 
            {
                "eventDelivery" : {
                "digital" : {
                    "secureid" : "8762871121"
                },
                "stateCode" : "Y-FINISH",
                "state" : "SUCCESS"            
            }, 
            {
                "eventDelivery" : {
                "digital" : {
                    "secureid" : "7651327152
                },
                "stateCode" : "Z-FINISH",
                "state" : "SUCCESS"            
            }
        ],
    }

需要使用 Java 阅读本文。

使用 3.x 版本的 Mongo Java 驱动程序 ...

MongoClient mongoClient = ...;

MongoCollection<Document> collection = mongoClient.getDatabase("...")
    .getCollection("...");

Bson filter = Filters.eq("eventDetails.stateCode", "Y-FINISH");

long count = collection.count(filter);