如何在Spring data Mongodb中使用ArrayOperation的大小类进行聚合?
这是我的Mongodb文档格式。我要做的是计算每个文档的发布数组长度。因此,对于这个文档,我的输出将是Count:2。我想用spring data mongodb实现这一点。我参考了这些文档,发现ArrayOperators类链接中有一个大小类: 但我是不知道用大小类 Mongodb终端查询为:如何在Spring data Mongodb中使用ArrayOperation的大小类进行聚合?,mongodb,aggregation-framework,spring-data-mongodb,Mongodb,Aggregation Framework,Spring Data Mongodb,这是我的Mongodb文档格式。我要做的是计算每个文档的发布数组长度。因此,对于这个文档,我的输出将是Count:2。我想用spring data mongodb实现这一点。我参考了这些文档,发现ArrayOperators类链接中有一个大小类: 但我是不知道用大小类 Mongodb终端查询为: { "_id" : ObjectId("5b4d975be2194da53a85e69f"), "ISBN" : 2, "book_title" : "ZERO TO ONE", "author_na
{
"_id" : ObjectId("5b4d975be2194da53a85e69f"),
"ISBN" : 2,
"book_title" : "ZERO TO ONE",
"author_name" : "Peter Theil",
"publisher" : "Penguin",
"status" : "ISSUED",
"issued" : [
{
"member_name" : "suresh",
"member_id" : 101,
"from_date" : "1/01/2018",
"to_date" : "15/02/2018"
},
{
"member_name" : "Jay",
"member_id" : 103,
"from_date" : "16/02/2018",
"to_date" : "30/03/2018"
}
]}
你可以试试下面的聚合 使用阵列运算符
db.library.aggregate([{$project:{count:{$size:"$issued"},"book_title":1,"_id":0}])
使用大小助手方法
ProjectionOperation project = Aggregation.
project("book_title").
andExclude("_id").
and(ArrayOperators.arrayOf("issued").length()).as("count");
聚合的其余部分
ProjectionOperation project = Aggregation.
project("book_title").
andExclude("_id").
and("issued").size().as("count");
Aggregation=Aggregation.newAggregation(项目);
List results=mongoTemplate.aggregate(聚合,“库”,Document.class).getMappedResults();
您可以尝试下面的聚合
使用阵列运算符
db.library.aggregate([{$project:{count:{$size:"$issued"},"book_title":1,"_id":0}])
使用大小助手方法
ProjectionOperation project = Aggregation.
project("book_title").
andExclude("_id").
and(ArrayOperators.arrayOf("issued").length()).as("count");
聚合的其余部分
ProjectionOperation project = Aggregation.
project("book_title").
andExclude("_id").
and("issued").size().as("count");
Aggregation=Aggregation.newAggregation(项目);
List results=mongoTemplate.aggregate(聚合,“库”,Document.class).getMappedResults();