我尝试与MongoDB存储库进行一些聚合。我使用了以下教程:https://www.mkyong.com/mongodb/spring-data-mongodb-aggregation-grouping-example/和https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.group。但是他们都没有工作,我不知道问题出在哪里。我的存储库看起来像:
<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0" created="2018-04-25T14:25:36.224Z">
<recording-list count="526823" offset="0">
...
</recording-list>
</metadata>
和MongoTable看起来像:
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();
}
问题是MongoTemplate给了我错误class MongoTable {
@Id
BigInteger id;
@Field
String skratka;
@Field
String zaciatok_studia;
@Field
int n;
...getter,setters, etc
,当我尝试将所有导入更改为静态导入时,它会给我: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
异常看起来问题在于AggregationRepository。聚合agg工作正常,但mongoTemplate为null。我错过了什么?
答案 0 :(得分:0)
将接口StudentRepository更改为简单的java类并删除扩展CrudRepository并添加方法返回:
List<MongoTable>
您尝试将bean注入接口,因此您得到了一个例外:
The blank final field mongoTemplate may not have been initialized