Update Query with annotation using Spring and MongoRepository

4.9k views Asked by At

I am using the latest version of Spring Boot and Spring Data MongoRepository. I have written a custom repository interface

    public interface CompanyRepository extends MongoRepository<Company, String>{

    @Query(value = "{ 'employer.userId' : ?0 }")
    Company findByCompanyUserUserId(String userId);
}

In the same way i want to use @Query annotation for updating a particular field. can someone suggest me?

2

There are 2 answers

0
Matt Vickery On

It's a reasonable question. Assuming that you're using the org.springframework.data.mongodb.repository.MongoRepository class, can you not simply use the insert(..) or save(..) methods for what you need?

API docs

1
vaibhavist On

Create an annotation like this:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MongoUpdate {

String find() default "{}";

String update() default "{}";

String collection();

boolean multi() default false;
}

And an aspect like this:

@Aspect
@Component
@SuppressWarnings("unchecked")
public class MongoUpdateAspect {

private static final Logger logger = LoggerFactory.getLogger(MongoUpdateAspect.class);

@Autowired
private MongoTemplate mongoTemplate;

@Pointcut("@annotation(com.ofb.commons.aop.common.MongoUpdate)")
public void pointCut() {
}

@Around("com.ofb.commons.aspect.MongoUpdateAspect.pointCut() && @annotation(mongoUpdate)")
public Object applyQueryUpdate(ProceedingJoinPoint joinPoint, MongoUpdate mongoUpdate) throws Throwable {
    Object[] args = joinPoint.getArgs();

    String findQuery = mongoUpdate.find();
    String updateQuery = mongoUpdate.update();
    String collection = mongoUpdate.collection();
    boolean multiUpdate = mongoUpdate.multi();

    for (int i = 0; i < args.length; i++) {
        if (args[i] instanceof Collection) {
            Collection collection1 = (Collection) args[i];
            String replaceStr = (String) collection1.stream().map(object -> {
                if (object instanceof Number) {
                    return object.toString();
                } else {
                    return String.format("\"%s\"", object.toString());
                }
            }).collect(Collectors.joining(","));
            findQuery = findQuery.replace(String.format("?%s", i), replaceStr);
            updateQuery = updateQuery.replace(String.format("?%s", i), replaceStr);
        } else if (args[i] instanceof Object[]) {
            Object[] objects = (Object[]) args[i];
            String replaceStr = Arrays.stream(objects).map(object -> {
                if (object instanceof Number) {
                    return object.toString();
                } else {
                    return String.format("\"%s\"", object.toString());
                }
            }).collect(Collectors.joining(","));
            findQuery = findQuery.replace(String.format("?%s", i), replaceStr);
            updateQuery = updateQuery.replace(String.format("?%s", i), replaceStr);
        } else {
            if (args[i] instanceof Number) {
                findQuery = findQuery.replace(String.format("?%s", i), args[i].toString());
                updateQuery.replace(String.format("?%s", i), args[i].toString());
            } else {
                findQuery = findQuery.replace(String.format("?%s", i), String.format("\"%s\"", args[i].toString()));
                updateQuery =
                    updateQuery.replace(String.format("?%s", i), String.format("\"%s\"", args[i].toString()));
            }
        }
    }

    Query query = new BasicQuery(findQuery);
    Update update = new BasicUpdate(updateQuery);

    if (multiUpdate) {
        mongoTemplate.updateMulti(query, update, collection);
    } else {
        mongoTemplate.updateFirst(query, update, collection);
    }
    return null;
   }
}

This will not work in MongoRepository implemented interfaces but you can create an empty bodied method in your service layer

@MongoUpdate(find = {}, update = "{$push : {'offFeatures' : ?0}}", collection = "userPreference", multi = true)
public void offFeatures(String feature) {

}