【MongoDB】MongoTemplate 操作 Aggregation 中添加 addFields

一、场景

Mongodb 的 sql,文档 wallpaper 的 _id 关联文档 wallpaper_get_success 的 wallpaper_id 进行查询。

db.wallpaper.aggregate([
    {
        $addFields: {
            "_id": {
                "$toString": "$_id"
            }
        }
    },
    {
      "$lookup": {
        "from": "wallpaper_get_success",
        "localField": "_id",
        "foreignField": "wallpaper_id",
        "as": "success_id"
      }
    },
    {
      "$match": {
        "success_id": {
          "$ne": []
        }
      }
    },
    {
      "$sample": {
        "size": 5
      }
    }
])

在MongoDB数据库进行操作 sql是可以的,但是在Java中使用MongoTemplate就不行,在newAggregation 中进行 lookup,match,sample 操作都可以,但是进行 addFields 操作就不行。

Aggregation aggregation = newAggregation(
                Aggregation.fields(""), //这一截是有问题的
                lookup("wallpaper_get_success", "_id", "wallpaper_id", "success_id"),
                match(Criteria.where("success_id").ne(new WallpaperGetSuccess[]{})),
                sample(5)
        );

在这里插入图片描述
原因:$addFields 在spring mongo中不支持

二、解决方法

1.example

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
Aggregation myDocAggr = newAggregation(
       match(Criteria.where("metaId.ref.uuid").is(someUUID)), 
       group("uuid").max("version").as("version"),
       lookup("simple","execId.ref.uuid","uuid","simple"),
       unwind("simple"),
       new AggregationOperation(){ 
         @Override 
         public Document toDocument(AggregationOperationContext aoc) {
            return new Document("$addFields",new Document("metaId.ref.name","$simple.name"));
         }
      }
)
List<Document> mydocumentList=mongoTemplate.aggregate(myDocAggr,"myDocument",Document.class).getMappedResults();

2.解决方法

把addFields 改成 new Document

关键点: 引入的 Document 类是,而不是 spring-boot-data-mongo 中的,改为import org.bson.Document;

import org.bson.Document;//关键的引入
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.*;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import org.springframework.data.mongodb.core.query.Collation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

Aggregation aggregation = newAggregation(
                aoc -> new Document("$addFields",new Document("_id",new Document("$toString","$_id"))),
                lookup("wallpaper_get_success", "_id", "wallpaper_id", "success_id"),
                match(Criteria.where("success_id").ne(new WallpaperGetSuccess[]{})),
                sample(5)
        );

不使用 lambda 表达式
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,如果你正在使用MongoDB数据库,并且是通过Spring Data MongoDBMongoTemplate进行操作的话,那么可以使用Aggregation.group()方法对数据进行分组聚合操作Aggregation.group()方法接受一个或多个字段作为参数,并将这些字段作为分组依据。例如,如果你想按照某个字段进行分组,可以这样写: ``` Aggregation.group("fieldName"); ``` 如果你想对某个字段进行特殊处理,可以使用Aggregation操作符,例如: 1. $sum:求和 ``` Aggregation.group("fieldName").sum("otherFieldName").as("total"); ``` 这个操作会对"otherFieldName"字段进行求和,并将结果保存在名为"total"的新字段。 2. $avg:求平均值 ``` Aggregation.group("fieldName").avg("otherFieldName").as("average"); ``` 这个操作会对"otherFieldName"字段进行求平均值,并将结果保存在名为"average"的新字段。 3. $max:求最大值 ``` Aggregation.group("fieldName").max("otherFieldName").as("maxValue"); ``` 这个操作会对"otherFieldName"字段进行求最大值,并将结果保存在名为"maxValue"的新字段。 4. $min:求最小值 ``` Aggregation.group("fieldName").min("otherFieldName").as("minValue"); ``` 这个操作会对"otherFieldName"字段进行求最小值,并将结果保存在名为"minValue"的新字段。 5. $push:将某个字段的值放入一个数组 ``` Aggregation.group("fieldName").push("otherFieldName").as("fieldValues"); ``` 这个操作会将"otherFieldName"字段的值放入一个名为"fieldValues"的数组。 6. $addToSet:将某个字段的值放入一个集合 ``` Aggregation.group("fieldName").addToSet("otherFieldName").as("fieldValues"); ``` 这个操作会将"otherFieldName"字段的值放入一个名为"fieldValues"的集合。 除了上述操作符之外,还有很多其他的操作符可以使用。你可以根据自己的需求进行选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值