@Override
public Question createQuestion(Question question) {
var existingQuestion = questionRepository.findByQuestionAndCourseAndLevelAndOptions(question.getQuestion(), question.getCourse(), question.getLevel().toString(), question.getOptions());
if (existingQuestion == null || !existingQuestion.equals(question)) {
var savedQuestion = questionRepository.save(question);
if (savedQuestion != null) {
var tags = savedQuestion.getTags();
List<Tag> newTags = new ArrayList<>();
List<UpdateOneModel<Tag>> updateModels = new ArrayList<>();
MongoCollection tagCollection = ((MongoCollection) tagRepository);
tags.forEach(tagName -> {
Document byTagName = (Document) tagCollection.find(eq("tagName", tagName)).first();
if (byTagName != null) {
updateModels.add(new UpdateOneModel<>(
eq("_id", byTagName.getClass()),
addToSet("questions", savedQuestion.getId())
));
} else {
newTags.add(new Tag(tagName, Collections.singleton(savedQuestion.getId())));
}
});
if (!newTags.isEmpty()) {
tagCollection.insertMany(newTags);
}
if (!updateModels.isEmpty()) {
tagCollection.bulkWrite(updateModels);
}
if (!newTags.isEmpty()) {
tagRepository.saveAll(newTags);
}
}
return question;
}
return null;
}
i am trying to add all tags data at once in mongo db using insertMany() but getting classCastException