子列表中的Mongo模板分页

时间:2018-06-12 16:13:03

标签: spring mongodb pagination aggregation mongotemplate

对于波纹管集合,我想根据城市检索街道的分页列表;

 {
   {   

    "city" : "city 1",
    "streets" : [ 
          "street name 1", 
          "street name 2", 
          "street name 3", 
          "street name 4"
          ]
   },
   {

    "city" : "city 2",
    "streets" : [ 
         "street name 2.1", 
         "street name 2.2", 
         "street name 2.3", 
         "street name 2.4"
        ]
    },
   {

    "city" : "city 3",
    "streets" : [ 
         "street name 3.1", 
         "street name 3.2", 
         "street name 3.3", 
         "street name 3.4"
        ]
    }
}

到目前为止,我有:

    List<AggregationOperation> list = new ArrayList<AggregationOperation>();
    list.add(Aggregation.unwind("streets"));
    list.add(Aggregation.match(Criteria.where("city").is("city 1")));

    list.add(Aggregation.group().push("streets").as("streets"));
    list.add(Aggregation.project("streets"));
    list.add(Aggregation.skip(0));
    list.add(Aggregation.limit(2));

    Aggregation agg = Aggregation.newAggregation(list);
    agg.skip(0);
    agg.limit(3);

    return (List<String>) mongoOperations.aggregate(agg, "myCityCollection", BasicDBObject.class)
            .getUniqueMappedResult().get("streets");

这将返回基于城市的街道列表,但不考虑跳过或限制。所有的街道都是城市1&#34;被检索。 是否有可能在街道上做分页?

0 个答案:

没有答案