Java Driver for MongoDB: Can't resolve function toJson()
<p>I wrote the following code based on the link
<a href="http://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/" rel="nofollow noreferrer">http://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/</a> (See title "Find All Documents in a Collection"):</p>

<pre><code>import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

public class Main {
    public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient();
        MongoDatabase database = mongoClient.getDatabase("test");
        MongoCollection collection = database.getCollection("test");
        MongoCursor cursor = collection.find().iterator();
        try {
           while(cursor.hasNext()) {
               System.out.println(cursor.next().toJson());
           }
        } finally {
           cursor.close();
        }
    }
}
</code></pre>

<p>However, I get an error that function toJson() can't be resolved. Do you have any idea how I can make this code work?</p>