MongoDB的SQL转化成mongoTemplate实现

db.Camera_5.aggregate([

	{
		$group:{
			_id: "any",
			camera:{
				$push: "$$ROOT"
			}
			
		}
	},
    {
        $lookup:{
            from:"Camera_6",
            localField:"invalidField",
            foreignField:"testField",
            as:"camera6"
        }
    },
		{
        $lookup:{
            from:"Camera_7",
            localField:"invalidField",
            foreignField:"testField",
            as:"camera7"
        }
    },
    {
        $project:{
            _id: 0,
            allValue:{
                $setUnion:["$camera","$camera6","$camera7"]
            }
        }
    },
		{
        $unwind:{
            path:"$allValue"
        }
    },
    {
        $project:{
            _id: "$allValue._id",
            CameraID:"$allValue.CameraID",
            DeviceName:"$allValue.DeviceName"
            
        }
    },
		{ $match : 
			{ 
				$and : [
					{ 
						CameraID : { $in : [111, 222]}
					}
				]
			}
		}

])

 

GroupOperation groupOperation = group().
                push("$$ROOT").as("camera");

final LookupOperation lookup = lookup("Camera_6", "invalidField", "testField", "camera6");

final LookupOperation lookup1 = lookup("Camera_7", "invalidField", "testField", "camera7");

final ProjectionOperation projectionOperation =
                project().andExclude("_id").and("camera").unionArrays("$camera6","$camera7").as("allValue");

final UnwindOperation allValue = unwind("allValue");

final ProjectionOperation projectionOperation1 =project()
                .andInclude("$allValue._id")
                .andInclude("$allValue.CameraID");

final MatchOperation match = match(multiConditionJoin(cameraPageQuery));

Sort cameraSort = Sort.sort(Camera.class);

Aggregation aggregation = newAggregation(
                            groupOperation,
                            lookup,
                            lookup1,
                            projectionOperation,
                            allValue,
                            projectionOperation1,
                            match,
                            TypedAggregation.sort(cameraSort),
                            skip(0),
                            limit(2)
                    );

System.out.println(aggregation);

final AggregationResults<Camera> aggregate = mongoTemplate.aggregate(aggregation, "Camera_5", Camera.class);

final CountOperation total = count().as("total");
                    Aggregation aggregation2 = newAggregation(
                            groupOperation,
                            lookup,
                            lookup1,
                            projectionOperation,
                            allValue,
                            projectionOperation1,
                            total
                    );

System.out.println(aggregation2);

final AggregationResults<PageTotal> totalNum = mongoTemplate.aggregate(aggregation2, "Camera_5", PageTotal.class);

单纯记录,写的时候有一些小坑,或者谈不上坑,因为mongoTemplate用的少,难免有些小问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值