Get random value from mongodb

I currently have a MongoDB database which has a collection named words which contains mango and apple for example. How can I get these randomised within my program. This needs to be implemented in the chooseRandomWord() method. Currently when running the application the output is “The chosen word is: com.mongodb.client.internal.Java8AggregateIterableImpl@2bbf4b8b “. I need to choose any random item from that collection. The values apple and mango in my database are called “name”. All the code is below:

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Aggregates;
import org.bson.Document;

import java.util.Arrays;
import java.util.Scanner;

public class Game {
    MongoClient client = MongoClients.create("mongodb://localhost:27017");
    MongoDatabase database = client.getDatabase("chosenWord");
    MongoCollection<Document> wordCollection = database.getCollection("words");
    //Player empty objects
    private final Player playerOne;
    private final Player playerTwo;
    private static final int maxScore = 5;
    private static String chosenWord;
    //Default is true but can alter BEFORE initialization of class
    public static Boolean canStartGame = true;
    //TODO: Try to get these from MongoDB?
    String[] words = new String[]
            {"banana", "apple", "pineapple",
                    "mango", "oranges", "watermelon"};

    public Game(Player playerOne, Player playerTwo) {
        this.playerOne = playerOne;
        this.playerTwo = playerTwo;
        //Set up chosen word and display

        if (canStartGame)

    private void init(){
        //Get chosen word from chooseRandomWord
        chosenWord = chooseRandomWord();

    //Players start making moves
    private void startMakingMoves(){

    //Display given word
    private void display(String chosenWord){
        System.out.println("The chosen word is: " + chosenWord);

    //TODO: Write test for getting random word
    public String chooseRandomWord() {
        return wordCollection.toString();
        //int randomNumber = new Random().nextInt(words.length);
        //return words[randomNumber];

    private void makeMove() {
        int i = 0;
        while (i <= maxScore) {
            //Player One
            System.out.print(playerOne.getName() + "'s move: ");
            if (playerOne.getScore() == maxScore)

            //Player Two
            System.out.print(playerTwo.getName() +"'s move: ");
            if (playerTwo.getScore() == maxScore)


    public void playerMove(Player player)  {
        Scanner scanner = new Scanner(;
        String playerGuess = "";


        boolean canRun = true;
        while(canRun) {
            playerGuess = scanner.nextLine();
            //TODO: Change this to check playerGuess is not null or a silly value using regex
            if (playerGuess.matches((".*?([a-z_]*\?+[a-z_]*).*?"))) {
                System.out.println("You must enter a valid guess");
            } else canRun = false;

        if (chosenWord.contains(playerGuess)) {
            int playerScore = player.getScore();
            System.out.println("That's correct, Your score is: " + player.getScore());
        } else {
            System.out.println("That's incorrect, Your score is: " + player.getScore());

    public String determineWinner(Player player) {
        int playerScore = player.getScore();
        String winnerText = "";

        if (playerScore == maxScore) {
            winnerText = player.getName() + " is the winner!";
        return winnerText;


public String chooseRandomWord() { 
    return wordCollection.aggregate(Arrays.asList(Aggregates.sample(1)))
        .map(doc -> doc.getString("name"))
        .into(new ArrayList<>()).get(0);