使用Spring和MongoRepository使用注释更新查询

时间:2016-12-20 13:50:32

标签: mongodb spring-boot spring-data-mongodb spring-repositories

我正在使用最新版本的Spring Boot和Spring Data MongoRepository。我编写了一个自定义存储库接口

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:30];
localNotification.repeatInterval = 900.0;
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];       

以同样的方式,我想使用@Query注释来更新特定字段。有人可以建议我吗?

2 个答案:

答案 0 :(得分:3)

创建一个这样的注释:

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

String find() default "{}";

String update() default "{}";

String collection();

boolean multi() default false;
}

这样的一个方面:

@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;
   }
}

这在MongoRepository实现的接口中不起作用,但您可以在服务层中创建一个空的身体方法

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

}

答案 1 :(得分:1)

这是一个合理的问题。假设你正在使用的org.springframework.data.mongodb.repository.MongoRepository类,你能不能简单地用刀片(..)或保存(..)方法,你需要什么?

API docs