我尝试使用 MongoDB 存储库进行一些聚合。我使用了这些教程:https://www.mkyong.com/mongodb/spring-data-mongodb-aggregation-grouping-example/和https://docs.spring.io/spring-data/mongodb/docs/current/参考/html/#mongo.group。但是它们都不起作用,我不知道问题出在哪里。我的存储库看起来像:
import java.math.BigInteger;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends CrudRepository<MongoStudent, String> {
@Autowired
MongoTemplate mongoTemplate;
Aggregation agg = newAggregation(project("studium"), unwind("studium"), group("studium").count().as("n"),
project("n").and("studium").previousOperation(), sort(Sort.Direction.DESC, "total"));
AggregationResults<MongoTable> results = MongoTemplate.aggregate(agg, "studium", MongoTable.class);
List<MongoTable> mappedResult = results.getMappedResults();
}
和 MongoTable 看起来像:
class MongoTable {
@Id
BigInteger id;
@Field
String skratka;
@Field
String zaciatok_studia;
@Field
int n;
...getter,setters, etc
问题是 MongoTemplate 给我错误Cannot make a static reference to the non-static method aggregate(Aggregation, String, Class<MongoTable>) from the type MongoTemplate
,当我尝试将所有导入更改为静态导入时,它给了我:The static import org.springframework.data.mongodb.core.MongoTemplate must be a field or member type
当我改变MongoTemplate.aggregate(agg, "studia", MongoTable.class);
它mongoTemplate.aggregate(agg, "studia", MongoTable.class);
时给我:The blank final field mongoTemplate may not have been initialized
我不知道还有什么尝试。有什么帮助吗?
有配置:
@EnableMongoRepositories
@Configuration
@ComponentScan(basePackages = "com.nosql")
public class MongoConfig extends AbstractMongoConfiguration{
static MongoClient mongo = null;
@Bean("applicationTemplate")
@Qualifier("applicationTemplate")
public MongoTemplate mongoTemplate(
final MappingMongoConverter mappingMongoConverter, final MongoClient mongoClient) {
final String databaseName = mongo.getDatabase("mynosqlproject").getName();
final MongoDbFactory dbFactory = new SimpleMongoDbFactory(mongoClient, databaseName);
return new MongoTemplate(dbFactory, mappingMongoConverter);
}
@Override
public MongoClient mongoClient() {
// Creating a Mongo client
MongoClientURI uri = new MongoClientURI(
"mongodb://xxx:xxx/mynosqlproject");
mongo = new MongoClient(uri);
return mongo;
}
@Override
protected String getDatabaseName() {
return mongo.getDatabase("mynosqlproject").getName();
}
}
更新:所以我的项目中有新课程。
public class AggregationRepository {
Aggregation agg = newAggregation( unwind("studium"), group("course","start").count().as("n"), sort(Sort.Direction.DESC,"n"));
@Autowired
MongoTemplate mongoTemplate;
AggregationResults<MongoTable> results = mongoTemplate.aggregate(agg, "studium",MongoTable.class);
List<MongoTable> mappedResult = results.getMappedResults();
}
和服务等级。
@Service
public class StudentService {
@Autowired
private StudentRepository repository;
private AggregationRepository repository2;
public void saveData() {
....
}
public void findStudentsFromYear() {
....
}
public void findStudentsDromCourse() {
......
}
public void AggreagationTable() {
List<MongoTable> mappedResult = repository2.mappedResult;
mappedResult.forEach(System.out::println);
}
}
问题是它仍然不起作用。它给了我一个nullpointer
例外,System.out.println(repository2.mappedResult);
看起来问题出在 AggregationRepository 中。聚合 agg 工作正常,但 mongoTemplate 为空。我想念什么?