You can easily query mongoDb using Spring Data MongoDB and maven as follows.
First you need to add maven dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Since it allows to map mongoDB document and a java POJO, create a model class.
import org.springframework.data.annotation.Id;
public class Player{
@Id
private String id;
private String playerName;
//getters and setters
}
Assuming your mongoDb document as follows
{
"_id": "123456789",
"playerName":"name1"
}
Then create an interface class as a repository by extending MongoRepository class
public interface PlayerRepository extends MongoRepository<Player, String> {
Player findById(String id);
Player findByPlayerName(String playerName);
@Query("{name:{$regex: ?0,$options:'i'}}")
List<Player> findPlayerByNameRegex(String name);
}
Finally you can use them Implementing or AutoWiring(recommended) the repository class. Just implement a method naming findByFiledName and remaining will do Spring MongoDb dependency. Additionally you can use @Query annotation for custom queries and filters.
Also you can refer the Spring Documentation