Reputation: 169
I have been querying mongodb using mongorepository and spring data.
My function looks like this:
@Repository
public interface UserRepository extends MongoRepository<User,String> {
@Query(value="{'email' : ?0}")
User findByEmail(String email);
}
My collection in mongodb looks like this
> db.user.find();
{ "_id" : "1", "email" : "[email protected]", "password" : "$2a$12$lDJgMZNLAcxv2J.QTZSjAuWJdPleBxXq.M4aj9itrR1RMDkgmwN7m", "name" : "def", "active" : 1, "isLoacked" : false, "isExpired" : false, "isEnabled" : true, "_class" : "com.x.gateway.auth.User" }
It always returns null value.
Upvotes: 2
Views: 2517
Reputation: 18410
Since email
is not a unique property, multiple users can be fetched. So map the method to List<User>
.
@Query(value="{'email' : ?0}")
List<User> findByEmail(String email);
Offical doc, here you can find more details.
Upvotes: 1
Reputation: 15878
Why are you using
@Query(value="{'email' : ?0}")
As email is the direct field (Not nested) , only
User findByEmail(String email);
is enough and should work.
Upvotes: 0