spring boot - How to fetch record on the basis of @DBRef in collection in mongodb? -
hi using mongodb springboot , not able fetch records on basis of @dbref. scenerio is:
i have authenticationtoken collection , user collection follows:
{ "_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")) }
and user
{ "_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") }
now want authentication token on basis of user id in authentication collection.
i using mongo repository fetch authenticationtoken on basis of user id not working.
to fetch authenticationtoken
step 1
public authenticationtoken findbyuserid(string userid){ objectid objid = new objectid(userid); return authrepository.findbyuserid(objid); }
step 2
public interface authenticationtokenrepository extends mongorepository<authenticationtoken, string> { authenticationtoken save(authenticationtoken token); authenticationtoken findbytoken(string token); @query("{'user._id' : ?0}") authenticationtoken findbyuserid(objectid objid); }
i following above steps fetch authenticationtoken db getting null. working fine when not using @dbref on user in authentication domain.
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; } }
this not big deal,use spring data mongodb criteria
query query = new query(criteria.where("user.$id").is(new objectid(userid))); authenticationtoken token = mongotemplate.findone(query, authenticationtoken.class);
Comments
Post a Comment