mongoTemplate 多条件嵌套查询并返回指定的数据

1. 之前通过query和DBObject查询 但是返回的结果啊是没被筛选下来的 可以通过聚合函数来实现 废话不多说上代码
2. List<AggregationOperation> operations = new ArrayList<AggregationOperation>(); operations.add(Aggregation.unwind("data")); operations.add(Aggregation.match(Criteria.where("data.step").is("20"))); operations.add(Aggregation.project("data.step").andExclude("_id")); Aggregation aggregation = Aggregation.newAggregation(operations); AggregationResults<String> results = mongoTemplate.aggregate(aggregation, "表名称", String.class);

4. 结果就不贴了因为之前写的代码 懒得运行了 这里粗略解释一下吧
5. 这里就是返回外层对象

operations.add(Aggregation.unwind(“data”));
这里就是对对象内容的筛选吧 我是这样理解的 单独执行命令的时候筛选的时候 返回的是data内的数据
operations.add(Aggregation.match(Criteria.where(“data.step”).is(“20”)));
这里是指定返回的参数 后面是不返回ID 默认是会返回id的
operations.add(Aggregation.project(“data.step”).andExclude("_id"));
如果想分页的话加上这俩句就好了 就可以得到你想要的

 operations.add(Aggregation.skip((currentPage - 1) * pageSize));
        operations.add(Aggregation.limit(pageSize));

这里是在mongodb原生的

db.aaa.aggregate([{"$unwind":"$data"}, {"$match":{"data.step":"20"}},{"$project":{"data.step":1,"_id":0}}])

{
step: “20”
}
这是返回的结果 就筛选出来了
下面是我的数据格式

 {
    "_id": ObjectId("5eb4fda6c25d000069007213"),
    "status": 0,
    "message": "",
    "supplierNum": "20140",
    "ipoNo": "14111",
    "data": [
        {
            "woNo": "4895",
            "technologyRoute": "sasfafa11",
            "step": "20",
            "entityId": "5944",
            "schedule": "1",
            "testTime": "2020-04-12 13:20:30",
            "content": {
                "a": {
                    "number": "211",
                    "secWinInden": "22"
                },
                "b": {
                    "ironLoss": "AAA"
                },
                "c": {
                    "resBatch": "666",
                    "resSeq": "AAA"
                },
                "d": {
                    "ironLoss": "aa",
                    "MagInducInten": "bb"
                }
            }
        },
        {
            "content": {
                "a": {
                    "number": "11",
                    "secWinInden": "22"
                },
                "B": {
                    "ironLoss": "AAA"
                },
                "c": {
                    "resBatch": "666",
                    "resSeq": "AAA"
                },
                "d": {
                    "ironLoss": "aa",
                    "MagInducInten": "bb"
                }
            },
            "entityId": "5944",
            "schedule": "1",
            "step": "30",
            "technologyRoute": "sasfafa11",
            "testTime": "2020-04-12 13:20:30",
            "woNo": "4895"
        }
    ]
}
 

写的也比较粗略 但是仔细看看可以看得懂

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值