使用java的嵌套数组的$elemMatch筛选器
我有JSON使用java的嵌套数组的$elemMatch筛选器,java,mongodb,Java,Mongodb,我有JSON { "responseCode": 200, "productList": { "count": 25, "products": [ { "id": 403492, "description": null, "plans": [ { "name": "test1", "type": "G" },
{
"responseCode": 200,
"productList": {
"count": 25,
"products": [
{
"id": 403492,
"description": null,
"plans": [
{
"name": "test1",
"type": "G"
},
{
"name": "test2",
"type": "Y"
}
]
}
],
}
}
我想得到关于“G”类型的计划,我不想把2测试回json
我尝试使用$elemMatch,但不起作用
Document query = new Document();
query.append("plans", new Document().append("$elemMatch", value));
很明显,是一种在数组元素与查询匹配时获取数据的查询方法
你想要的是!但是,根据该报告,这一预测存在一个问题:
$运算符和$elemMatch运算符都会投影第一个
基于条件匹配数组中的元素
这意味着您将只获得与查询匹配的第一个元素,
所以也许它不符合你的需要
有两种方法:
1-要在应用程序端投影您想要的数据,可以使用java stream api
2-使用
第二种方式(java代码):
import com.mongodb.client.MongoClient;
导入com.mongodb.client.MongoClients;
导入com.mongodb.client.MongoCollection;
导入com.mongodb.client.MongoDatabase;
导入org.bson.Document;
导入org.bson.conversions.bson;
导入java.util.array;
导入静态com.mongodb.client.model.Aggregates.match;
导入静态com.mongodb.client.model.Aggregates.project;
导入静态com.mongodb.client.model.Filters.*;
公开课考试{
公共静态void main(字符串[]args)引发异常
{
MongoClient=MongoClient.create(
"mongodb://localhost:27017"
);
MongoDatabase数据库=client.getDatabase(“测试”);
MongoCollection文档MongoCollection=
database.getCollection(“test”);
Document Document=documentMongoCollection.aggregate(
Arrays.asList(
匹配(elemMatch(“productionList.products.plans”,等式(“type”,“G”))
,项目(新文档(“productionList.products”),
映射(“productionList.products”,“this”,
新文件(“计划”,
过滤器($this.plans),
“计划”,
新文件(
“$eq”,
Arrays.asList(“$$plan.type”,“G”)
)))
.append(“description”,“$$this.description”)//将其呈现
.append(“id”,“$$this.id”)//呈现它
.append(“responseCode”,1)//包括
.append(“productionList.count”,1)//包含
)
)).first();
System.out.println(document.toJson());
}
专用最终静态Bson筛选器(字符串arrayName、字符串as、Bson cond)
{
//这么做吧!
文档=新文档(“输入”、“$”+arrayName);
if(as!=null&!as.isEmpty())
文件。附加(“as”,as);
文件。附加(“cond”,cond);
返回新文档(“$filter”,文档);
}
专用最终静态Bson映射(字符串arrayName、字符串as、Bson in)
{
文档=新文档(“输入”、“$”+arrayName);
if(as!=null&!as.isEmpty())
文件。附加(“as”,as);
文件。附加(“in”,in);
返回新文档(“$map”,文档);
}
}
可能很难理解我在这段代码上写了什么,您需要检查以下链接:
投影运算符不能用于两个级别的嵌套数组。从嵌套数组的第二级获取匹配数组元素的方法是使用一个
db.collection.aggregate( [
{ $unwind: "$productList.products" },
{ $unwind: "$productList.products.plans" },
{ $match: { "productList.products.plans.type": "G" } },
] )
这转化为以下Java代码:
MongoClient mongoClient = MongoClients.create("mongodb://localhost/");
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("collection");
List<Bson> pipeline = Arrays.asList(
unwind("$productList.products"),
unwind("$productList.products.plans"),
match(eq("productList.products.plans.type", "G"))
);
List<Document> results = new ArrayList<>();
collection.aggregate(pipeline).into(results);
results.forEach(doc -> System.out.println(doc.toJson()));
MongoClient MongoClient=MongoClients.create(“mongodb://localhost/");
MongoDatabase=mongoClient.getDatabase(“测试”);
MongoCollection collection=database.getCollection(“collection”);
List pipeline=Arrays.asList(
展开($productList.products”),
展开($productList.products.plans”),
匹配(等式(“productList.products.plans.type”,“G”))
);
列表结果=新建ArrayList();
集合。聚合(管道)。导入(结果);
results.forEach(doc->System.out.println(doc.toJson());
放松不能满足他的需要。正如他所说,他将获取相同的json结构!
MongoClient mongoClient = MongoClients.create("mongodb://localhost/");
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("collection");
List<Bson> pipeline = Arrays.asList(
unwind("$productList.products"),
unwind("$productList.products.plans"),
match(eq("productList.products.plans.type", "G"))
);
List<Document> results = new ArrayList<>();
collection.aggregate(pipeline).into(results);
results.forEach(doc -> System.out.println(doc.toJson()));