gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:52:49 24 4
gpt4 key购买 nike

我需要找到 eventDetails.eventDelivery.stateCode = "Y-FINISH" 的记录/计数。它可以位于数组列表中的任何元素中。

只想找到 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);

关于java - 如何使用 Java 从 MongoDB 读取嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47774735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com