简体   繁体   中英

Mongodb @DBRef query spring

I my spring project, I am using two models with a reference.

A model user which has a reference on a token object.

@Document(collection = "user")
public class User {

   @Id
   public String id;
   @DBRef
   public Token token;
}

Then my Token object:

@Document(collection = "token")
public class Token {

   @Id
   public String id;
   public String token;
}

I am trying to query a user from a token. I tried to create a MongoRepository interface :

@Repository
public interface UserRepository extends MongoRepository<User, String> {
    @Query(value="{ 'token.id' : ?0 }")
    User findByTokenId(String id);
}

But that's not working. How can I create such kind or request ?

you can use:

@Repository
public interface UserRepository extends MongoRepository<User, String> {
    @Query(value="{ 'token.id' : ?0 }")
    User findAllByToken_Id(String id);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM