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”: {
KaTeX parse error: Expected '}', got 'EOF' at end of input: … input: "contentList",
as: “item”,
cond: { e q : [ " eq : [" eq:["$item.name",“皇冠梨”] }
}
}
}
},{
$sort:{“name”:-1
}
},{
KaTeX parse error: Expected 'EOF', got '}' at position 17: …imit: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 : { KaTeX parse error: Expected '}', got 'EOF' at end of input: …// {and:[{“schedulerContentList.name”: “皇冠梨” },{“customer”:“9527”}]},
// {$and:[{“name”:“AA”},{“customer” : “9527”}]}
// ]
// }
// },
// {
// $project: {
// “customer”: 1,
// “name”: 1,
// “schedulerContentList”: {
// KaTeX parse error: Expected '}', got 'EOF' at end of input: … input: "schedulerContentList",
// as: “item”,
// cond: { e q : [ " eq : [" eq:["$item.name",“皇冠梨”] }
// }
// }
// }
// },{
// $sort:{“name”:-1
// }
// },{
// KaTeX parse error: Expected 'EOF', got '}' at position 20: …t:20 // }̲,{skip:0}
//
// ]
//)

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

    //拼接条件

// Document sub_match = new Document();
List match_or_list= new ArrayList<>();
Document match_or = new Document(“ o r " , m a t c h o r l i s t ) ; D o c u m e n t m a t c h = n e w D o c u m e n t ( " or",match_or_list); Document match = new Document(" or",matchorlist);Documentmatch=newDocument("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(“ f i l t e r " , / / n e w D o c u m e n t ( ) / / . a p p e n d ( " i n p u t " , " filter", // new Document() // .append("input", " filter",//newDocument()//.append("input","contentList”)
// .append(“as”, “item”)
// .append(“cond”, new Document(“ e q " , A r r a y s . a s L i s t ( " eq", Arrays.asList(" 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 嵌套#################################

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值