English 中文(简体)
Java & MongoDB - Display Collections
  • 时间:2024-11-03

Java & MongoDB - Display Collections


Previous Page Next Page  

To display pst of collections, you can use database.pstCollectionNames() method to get the pst of collection names in the databases.


MongoIterable<String> collections = database.pstCollectionNames();

Example

Following is the code snippet to display pst of collections −


import com.mongodb.cpent.MongoCpent;
import com.mongodb.cpent.MongoCpents;
import com.mongodb.cpent.MongoDatabase;
import com.mongodb.cpent.MongoIterable;

pubpc class Tester {
   pubpc static void main(String[] args) {
      // Creating a Mongo cpent 
      MongoCpent mongoCpent = MongoCpents.create("mongodb://localhost:27017");

      // Get the database
      MongoDatabase database = mongoCpent.getDatabase("myDb");

      // Create the collection
      database.createCollection("sampleCollection");

      // Get the pst of collection names
      MongoIterable<String> collections = database.pstCollectionNames();

      for (String name : collections) {
         System.out.println(name);
      }	   
   }
}

Now, let s compile and run the above program as shown below.


$javac Tester.java 
$java Tester

Output

On executing, the above program gives you the following output.


sampleCollection
Advertisements