title: How to fetch record on the basis of @DBRef in collection in mongodb?
body: <p>Hi i am using mongodb with springboot and not able to fetch records on the basis of @DBRef. My scenerio is:</p>
  
  <p>I have AuthenticationToken Collection and User collection as follows:</p>
  
  <pre><code>{
      "_id" : ObjectId("556bdfc2ccf2e6509f8a2849"),
      "_class" : "com.samepinch.domain.user.AuthenticationToken",
      "token" : "2efd1cfe-2f2f-4163-b500-bac6e4654287",
      "createdDate" : ISODate("2015-06-01T04:29:54.364Z"),
      "updatedDate" : ISODate("2015-06-01T04:29:54.364Z"),
      "user" : DBRef("users", ObjectId("556bdfc2ccf2e6509f8a2848"))
  }
  </code></pre>
  
  <p>And User</p>
  
  <pre><code>{
      "_id" : ObjectId("556bdfc2ccf2e6509f8a2848"),
      "_class" : "com.samepinch.domain.user.User",
      "age" : 0,
      "username" : "abc@yahoo.com",
      "roles" : [
          "ROLE_USER"
      ],
      "firstName" : "abc",
      "lastName" : "mno",
      "email" : "abc@yahoo.com",
      "gender" : "male",
      "isAccountLocked" : false,
      "prefAgeFrom" : 0,
      "prefAgeTo" : 0,
      "notificationNewMatch" : true,
      "notificationMessage" : true,
      "createdDate" : ISODate("2015-06-01T04:29:54.325Z"),
      "updatedDate" : ISODate("2015-06-01T04:29:54.325Z")
  }
  </code></pre>
  
  <p>Now i want to get Authentication token on the basis of user id in authentication collection.</p>
  
  <p>I am using Mongo Repository to fetch AuthenticationToken on the basis of user id but it is not working.</p>
  
  <p>To fetch AuthenticationToken</p>
  
  <p>Step 1</p>
  
  <pre><code>public AuthenticationToken findByUserId(String userId){
        ObjectId objId =  new ObjectId(userId);
          return authRepository.findByUserId(objId);
      }
  </code></pre>
  
  <p>Step 2</p>
  
  <pre><code>public interface AuthenticationTokenRepository extends MongoRepository&lt;AuthenticationToken, String&gt; {
  
      AuthenticationToken save(AuthenticationToken token);
  
      AuthenticationToken findByToken(String token);
  
      @Query("{'user._id' : ?0}")
      AuthenticationToken findByUserId(ObjectId objId);
  }
  </code></pre>
  
  <p>I am following above steps to fetch AuthenticationToken from DB but getting null. Previously it was working fine when i was not using @DBRef over User in Authentication Domain.</p>
  
  <p>AuthenticationToken</p>
  
  <pre><code>public class AuthenticationToken extends BaseEntity{
  
      @JsonProperty
      String token;
  
      @DBRef
      User user;
  
      public AuthenticationToken(String token,User user){
          this.token = token;
          this.user = user;
      }
  
      public User getUser() {
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
      public String getToken() {
          return token;
      }
  
      public void setToken(String token) {
          this.token = token;
      }
  
  
  
  }
  </code></pre>