如何在Mongodb Spring

时间:2016-08-09 08:26:18

标签: spring mongodb aggregation-framework

我在MongoDb中有一个关于

的集合
{
 "_id" : ObjectId("5756b0rrffac18767"),
 "year" : "1992",
 "initial" : "A",
  "name" : "test"

}

/* 2 */
{
  "_id" : ObjectId("5756b0ffac18767"),
 "year" : "1992",
 "initial" : "B",
  "name" : "test"
}

 /* 3 */
{
 "_id" : ObjectId("5756b0ffwwac18767"),
 "year" : "1993",
 "initial" : "A",
  "name" : "test1"
}

/* 4 */
{
 "_id" : ObjectId("5756b0ffacee767"),
 "year" : "1992",
 "initial" : "A",
  "name" : "test"
}

我需要获得以下结果,如

1992-A
1992-B

我需要找到不同的连接列(年份和首字母),其中name =“test”

到目前为止,我尝试了

public List<MyNames> getYears(String name) {
   TypedAggregation<MyNames> agg = Aggregation.newAggregation( MyNames.class,
            project("id","year", "initial"),
            match(Criteria.where("name").is(name))
            group("year")

        ); 

        AggregationResults<MyNames> result = mongoTemplate.aggregate(agg, MyNames.class);
        List<MyNames> resultList = result.getMappedResults();
     return resultList;
  }

MyNames

@Document

公共类MyNames实现Serializable {

private static final long serialVersionUID = -5763434erertr33331L;

@Id
private String id;
private int year;
private String name;
private String initial;

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public int getYear() {
    return year;
}
public void setYear(int year) {
    this.year = year;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getInitial() {
    return initial;
}
public void setInitial(String initial) {
    this.initial = initial;
}

@Override
public String toString() {
    return "MyNames [id=" + id + ", year=" + year + ", name=" + name + ", initial=" + initial + "]";
 }
}

这不起作用..感谢任何帮助..

1 个答案:

答案 0 :(得分:1)

这是代码。请相应更改集合名称。

包括年份和首字母之间的连字符。

public Boolean aggregateYearCollection(String name) {

        MongoOperations mongoOperations = getMongoConnection();

        MatchOperation match = new MatchOperation(Criteria.where("name").is(name));
        ProjectionOperation project = Aggregation.project("year", "initial").andExpression("concat(year, '-', initial)").as("yearAndInitial");
        Aggregation aggregate = Aggregation.newAggregation(match, project, Aggregation.group("yearAndInitial"));

        System.out.println(aggregate.toString());

        AggregationResults<YearCollection> yearCollectionAggregate = mongoOperations.aggregate(aggregate, "yearcoll", YearCollection.class);

        if (yearCollectionAggregate!=null) {
            System.out.println("Output ====>" + yearCollectionAggregate.getRawResults().get("result"));
            System.out.println("Output ====>" + yearCollectionAggregate.getRawResults().toMap());
        }

        return true;

    }

<强>输出: -

Output ====>[ { "_id" : "1992-A"} , { "_id" : "1992-B"}]
Output ====>{serverUsed=127.0.0.1:27017, waitedMS=0, result=[ { "_id" : "1992-A"} , { "_id" : "1992-B"}], ok=1.0}

Aggregate API没有为您提供列表。但是,您可以通过迭代数据来获取ID列表。

Iterator<YearCollection> yearCollectionIterator = yearCollectionAggregate.iterator();

List<String> valueList = new ArrayList<String>();

while(yearCollectionIterator.hasNext()) {
    valueList.add(yearCollectionIterator.next().getId());
}