MongoDB 和 Spring:如何在一个存储库查询中正确查询两个字段?

MongoDB and Spring: How to correctly query with two fields in one repository query?

我有以下 POJO:

    public class Round {

        private ObjectId _id;

        @NotEmpty
        @Getter
        @Setter
        @Accessors(fluent = true)
        @JsonProperty("userId")
        private String userId;

        // rest of fields

}

在我的 Spring 引导项目中,我试图通过 userId 和 _id 查询我的 mongoDB,如下所示:

@Repository
public interface RoundRepository extends MongoRepository<Round, String> {

    Optional<Round> findByUserIdAnd_id(String userId, ObjectId objectId);

}

然而,当我尝试 gradlew bootRun 我现在得到:

Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'roundRepository': Invocation of init method failed; nested exception is 
org.springframework.data.mapping.PropertyReferenceException: No property and found for type String! Traversed path: Round.userId.

使用 2 个参数查询 MongoDB正确 方法是什么?

你必须在 Round class 中使用注解 @Document 和 ID 属性的注解 @Id:

@Document
public class Round {


        @Id
        private ObjectId _id;

        @NotEmpty
        @Getter
        @Setter
        @Accessors(fluent = true)
        @JsonProperty("userId")
        private String userId;

        // rest of fields

}