Я использую Spring-data-mongodb для подключения к MongoDb из своего веб-приложения Java. Мне нужно использовать группу (агрегацию в mongoDB) для нескольких запросов.
Я использую следующий код для агрегации.
MongoTemplate mongoTemplate=getMongoTemplate();
public List<SummaryVo> getGroupResult(Appointment appointment, String doctor_id) {
System.out.println(mongoTemplate.toString());
Aggregation aggregation = newAggregation(
match(Criteria.where("clinic_name").is(appointment.getClinic_name()).and("appointment_date_time")
.gte(appointment.getAppointment_date_time()).lte(appointment.getEnd_appointment_date_time())),
group(doctor_id).count().as("total"),
project("total").and(doctor_id).previousOperation(),
sort(Sort.Direction.DESC, "total")
);
//Convert the aggregation result into a List
AggregationResults<SummaryVo> groupResults
= mongoTemplate.aggregate(aggregation, "appointment", SummaryVo.class);
return groupResults.getMappedResults();
}
public MongoTemplate getMongoTemplate(){
final UserCredentials userCredentials = new UserCredentials("admin", "admin123");
final MongoTemplate mongoTemplate = new MongoTemplate(new Mongo(),"my-database", userCredentials);
return mongoTemplate;
}
Первоначально я автоматически подключил Mongotemplate напрямую, и он работал нормально. Теперь я добавил аутентификацию в My mongoDb, и все остальные мои запросы работают, кроме этого агрегированного метода.
I am trying to find a way to solve it. Thats why I tried using mongoTemplate with user credential so that it can authenticate the DB. But still its not working.I am getting belo mentioned error in tomcat log:-
Caused by: com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on cms-database to execute command { aggregate: "appointment", pipeline: [ { $match: