2

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

    public interface CompanyRepository extends MongoRepository<Company, String>{

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

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

4

2 回答 2

4

像这样创建注释:

@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) {

}
于 2017-11-04T09:36:37.943 回答
1

这是一个合理的问题。假设你正在使用 org.springframework.data.mongodb.repository.MongoRepository 类,你能不能简单地使用 insert(..) 或 save(..) 方法来满足你的需要?

API 文档

于 2016-12-23T11:56:52.850 回答