如何根据@DBRef 获取mongodb 集合中的记录?
How to fetch record on the basis of @DBRef in collection in mongodb?
您好,我正在将 mongodb 与 springboot 一起使用,但无法基于 @DBRef 获取记录。我的场景是:
我有如下的 AuthenticationToken 集合和 User 集合:
{
"_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"))
}
和用户
{
"_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")
}
现在我想根据身份验证集合中的用户 ID 获取身份验证令牌。
我正在使用 Mongo 存储库根据用户 ID 获取 AuthenticationToken,但它不起作用。
获取 AuthenticationToken
第 1 步
public AuthenticationToken findByUserId(String userId){
ObjectId objId = new ObjectId(userId);
return authRepository.findByUserId(objId);
}
第 2 步
public interface AuthenticationTokenRepository extends MongoRepository<AuthenticationToken, String> {
AuthenticationToken save(AuthenticationToken token);
AuthenticationToken findByToken(String token);
@Query("{'user._id' : ?0}")
AuthenticationToken findByUserId(ObjectId objId);
}
我正在按照上述步骤从数据库中获取 AuthenticationToken,但结果为空。以前当我没有在身份验证域中对用户使用 @DBRef 时它工作正常。
AuthenticationToken
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;
}
}
这没什么大不了的,使用 spring 数据 mongodb 标准,例如
Query query = new Query(Criteria.where("user.$id").is(new ObjectId(userId)));
AuthenticationToken token = mongoTemplate.findOne(query, AuthenticationToken.class);
您好,我正在将 mongodb 与 springboot 一起使用,但无法基于 @DBRef 获取记录。我的场景是:
我有如下的 AuthenticationToken 集合和 User 集合:
{
"_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"))
}
和用户
{
"_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")
}
现在我想根据身份验证集合中的用户 ID 获取身份验证令牌。
我正在使用 Mongo 存储库根据用户 ID 获取 AuthenticationToken,但它不起作用。
获取 AuthenticationToken
第 1 步
public AuthenticationToken findByUserId(String userId){
ObjectId objId = new ObjectId(userId);
return authRepository.findByUserId(objId);
}
第 2 步
public interface AuthenticationTokenRepository extends MongoRepository<AuthenticationToken, String> {
AuthenticationToken save(AuthenticationToken token);
AuthenticationToken findByToken(String token);
@Query("{'user._id' : ?0}")
AuthenticationToken findByUserId(ObjectId objId);
}
我正在按照上述步骤从数据库中获取 AuthenticationToken,但结果为空。以前当我没有在身份验证域中对用户使用 @DBRef 时它工作正常。
AuthenticationToken
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;
}
}
这没什么大不了的,使用 spring 数据 mongodb 标准,例如
Query query = new Query(Criteria.where("user.$id").is(new ObjectId(userId)));
AuthenticationToken token = mongoTemplate.findOne(query, AuthenticationToken.class);