MongoDB 数组查询(数组中符合条件返回)+分页+排序

数据创建
db.arrayquery.insert([
{
	"_id" : NumberLong(3),
	"name" : "n1",
	"creator" : "c3",
	"contentList" : [
		{
			"name" : "皇冠梨3",
			"hover" : 20,
			"content" : {
				"source" : "s1",
				"mId" : "m0001",
				"name" : "皇冠梨",
				"type" : 2
			}
		},
		{
			"name" : "模板2",
			"hover" : 10,
			"content" : {
				"source" : "s1",
				"mId" : "m0001",
				"name" : "模板2",
				"type" : 2
			}
		},
		{
			"name" : "10寸西红柿",
			"hover" : 13,
			"content" : {
				"source" : "s2",
				"mId" : "m0001",
				"name" : "10寸西红柿",
				"type" : 2
			}
		}
	],
	"gmtCreate" : NumberLong("2718064537"),
	"gmtModified" : NumberLong("2718064537"),
	"serial" : 1,
	"priority" : 20
},

{
	"_id" : NumberLong(2),
	"name" : "n1",
	"creator" : "c2",
	"contentList" : [
		{
			"name" : "皇冠梨",
			"hover" : 20,
			"content" : {
				"source" : "s1",
				"mId" : "m0001",
				"name" : "皇冠梨",
				"type" : 2
			}
		},
		{
			"name" : "模板2",
			"hover" : 10,
			"content" : {
				"source" : "s1",
				"mId" : "m0001",
				"name" : "模板2",
				"type" : 2
			}
		},
		{
			"name" : "10寸西红柿",
			"hover" : 13,
			"content" : {
				"source" : "s2",
				"mId" : "m0001",
				"name" : "10寸西红柿",
				"type" : 2
			}
		}
	],
	"gmtCreate" : NumberLong("2718064537"),
	"gmtModified" : NumberLong("2718064537"),
	"serial" : 1,
	"priority" : 20
},

{
	"_id" : NumberLong(1),
	"name" : "n1",
	"creator" : "c1",
	"contentList" : [
		{
			"name" : "皇冠梨",
			"hover" : 20,
			"content" : {
				"source" : "s1",
				"mId" : "m0001",
				"name" : "皇冠梨",
				"type" : 2
			}
		},
		{
			"name" : "模板2",
			"hover" : 10,
			"content" : {
				"source" : "s1",
				"mId" : "m0001",
				"name" : "模板2",
				"type" : 2
			}
		},
		{
			"name" : "10寸西红柿",
			"hover" : 13,
			"content" : {
				"source" : "s2",
				"mId" : "m0001",
				"name" : "10寸西红柿",
				"type" : 2
			}
		}
	],
	"gmtCreate" : NumberLong("2718064537"),
	"gmtModified" : NumberLong("2718064537"),
	"serial" : 1,
	"priority" : 20
}]);

数据查询
db.arrayquery.aggregate(
    [
        { 
            $match : { "contentList.name": "皇冠梨" } 
        },
        {
            $project: {
                "customer": 1,
                "name": 1,
                "contentList": {
                    $filter: {
                        input: "$contentList",
                        as: "item",
                        cond: { $eq : ["$$item.name","皇冠梨"] }
                    }
                }
            }
        },{
        	$sort:{"name":-1
        	}
        },{
        	$limit:3
        },{$skip:0}
        
    ]
)
代码实现
  @RequestMapping("/test")
    @ResponseBody
    public Object test() throws Exception {

        List<Document> aggregateList = new ArrayList<>();

        //拼接条件
        Document sub_match = new Document();
        Document match = new Document("$match", sub_match);
        sub_match.append("contentList.name", "皇冠梨");
        aggregateList.add(match);
        //显示数据
        Document sub_project = new Document();
        sub_project.append("customer", 1);
        sub_project.append("name", 1);
        sub_project.append("contentList"
                , new Document("$filter",
                        new Document()
                                .append("input", "$contentList")
                                .append("as", "item")
                                .append("cond", new Document("$eq", Arrays.asList("$$item.name", "皇冠梨"))))

        );


        Document project = new Document("$project", sub_project);
        aggregateList.add(project);
        Document sort_project = new Document("name", -1);
        Document sort = new Document("$sort", sort_project);
        aggregateList.add(sort);


        aggregateList.add(new Document("$limit",1));
        aggregateList.add(new Document("$skip",0));



        AggregateIterable aggregateIterable = mongoTemplate.getCollection("arrayquery").aggregate(aggregateList).allowDiskUse(true);
        MongoCursor cursor = aggregateIterable.iterator();
        List<Object> list = new ArrayList<>();
        while (cursor.hasNext()) {
            Object object = cursor.next();
            list.add(object);
            System.out.println(JSON.toJSONString(object));
        }
        return list;
    }






#############################start $and $or 嵌套#################################

//        db.getCollection('marketingScheduler').aggregate(
//                [
//                {
//                        $match : { $or:[
//            {$and:[{"schedulerContentList.name": "皇冠梨" },{"customer":"9527"}]},
//            {$and:[{"name":"AA"},{"customer" : "9527"}]}
//                ]
//        }
//        },
//        {
//            $project: {
//                "customer": 1,
//                        "name": 1,
//                        "schedulerContentList": {
//                    $filter: {
//                        input: "$schedulerContentList",
//                                as: "item",
//                                cond: { $eq : ["$$item.name","皇冠梨"] }
//                    }
//                }
//            }
//        },{
//            $sort:{"name":-1
//            }
//        },{
//            $limit:20
//        },{$skip:0}
//
//    ]
//)

        List<Document> aggregateList = new ArrayList<>();

        //拼接条件
//        Document sub_match = new Document();
        List<Document> match_or_list= new ArrayList<>();
        Document match_or = new Document("$or",match_or_list);
        Document match = new Document("$match", match_or);

        List<Document> match_or_list_and1_list= new ArrayList<>();
        Document match_or_list_and1 = new Document("$and", match_or_list_and1_list);
        match_or_list_and1_list.add(new Document().append("schedulerContentList.name","皇冠梨" ).append("customer","9527"));


        List<Document> match_or_list_and2_list= new ArrayList<>();
        Document match_or_list_and2 = new Document("$and", match_or_list_and2_list);
        match_or_list_and2_list.add(new Document().append("name","AA" ).append("customer","9527"));


        match_or_list.add(match_or_list_and1);
        match_or_list.add(match_or_list_and2);

        aggregateList.add(match);
        //显示数据
        Document sub_project = new Document();
        sub_project.append("customer", 1);
        sub_project.append("name", 1);
//        sub_project.append("contentList"
//                , new Document("$filter",
//                        new Document()
//                                .append("input", "$contentList")
//                                .append("as", "item")
//                                .append("cond", new Document("$eq", Arrays.asList("$$item.name", "皇冠梨"))))
//
//        );7


        Document project = new Document("$project", sub_project);
        aggregateList.add(project);
        Document sort_project = new Document("name", -1);
        Document sort = new Document("$sort", sort_project);
        aggregateList.add(sort);


        aggregateList.add(new Document("$limit",10));
        aggregateList.add(new Document("$skip",0));



        AggregateIterable aggregateIterable = mongoTemplate.getCollection("marketingScheduler").aggregate(aggregateList).allowDiskUse(true);
        MongoCursor cursor = aggregateIterable.iterator();
        List<Object> list = new ArrayList<>();
        while (cursor.hasNext()) {
            Object object = cursor.next();
            list.add(object);
            System.out.println(JSON.toJSONString(object));
        }
        return list;
#############################end $and $or 嵌套#################################

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值