Mongo数据库存储库 - 聚合 - Mongo 模板不工作
MongoDB repository - Aggregation - Mongo Template dont work
我尝试使用 MongoDB 存储库进行一些聚合。我使用了这些教程:https://www.mkyong.com/mongodb/spring-data-mongodb-aggregation-grouping-example/ and https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.group。但是其中 none 不起作用,我不知道问题出在哪里。我的存储库看起来像:
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();
}
}
更新: 所以我的项目中有新的 classes。
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();
}
和服务class。
@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);
}
}
问题是它仍然不起作用。它在 System.out.println(repository2.mappedResult);
处给我 nullpointer
异常 看起来问题出在 AggregationRepository 中。聚合 agg 工作正常,但 mongoTemplate 为空。我想念什么?
将接口 StudentRepository 更改为简单 java class 并删除扩展 CrudRepository 并添加方法 returns:
List<MongoTable>
您尝试将一个 bean 注入到接口中,因此您得到了一个异常:
The blank final field mongoTemplate may not have been initialized
我尝试使用 MongoDB 存储库进行一些聚合。我使用了这些教程:https://www.mkyong.com/mongodb/spring-data-mongodb-aggregation-grouping-example/ and https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.group。但是其中 none 不起作用,我不知道问题出在哪里。我的存储库看起来像:
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();
}
}
更新: 所以我的项目中有新的 classes。
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();
}
和服务class。
@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);
}
}
问题是它仍然不起作用。它在 System.out.println(repository2.mappedResult);
处给我 nullpointer
异常 看起来问题出在 AggregationRepository 中。聚合 agg 工作正常,但 mongoTemplate 为空。我想念什么?
将接口 StudentRepository 更改为简单 java class 并删除扩展 CrudRepository 并添加方法 returns:
List<MongoTable>
您尝试将一个 bean 注入到接口中,因此您得到了一个异常:
The blank final field mongoTemplate may not have been initialized