java mongoTemplate操作 Aggregation中添加addFields

mongoTemplate操作mongodb真是各种坑,springboot官方文档上给到的案例不满足实际的需求

场景复现

现在有条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连接查询,localField使用_id进行匹配不生效

{
        $addFields: {
            "_id": {
                "$toString": "$_id"
            }
        }
    }

但是数据库操作很sql,转换成Java代码就日狗了,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)
        );

image.png

网上对于这个的操作几乎没有,后来还是在stackoverflow找到的

解决办法

原文:How can I implement addFields mongoDB query in Java

其实也就是,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表达式,如下

image.png

这里比较一下原生sql和java代码

image.png

# mongodb 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×