public static void main(String args[]) {

    // MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
    MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
    MongoClient mongoClient = new MongoClient(connectionString);
    MongoDatabase database = mongoClient.getDatabase("PROJ");

    MongoCollection<Document> collection = database.getCollection("proj");

    // *************************************************
    // permet de compter le nombre d'élément dans la base
    // **************************************************
    //        System.out.print(collection.count());

    // ************************************
    // permet d'afficher toutes les donnés
    // ************************************
    //        FindIterable<Document> iterable = collection.find();
    //        iterable.forEach(new Block<Document>() {
    //            @Override
    //            public void apply(final Document document) {
    //                System.out.println(document);
    //            }
    //        });

    // ************************************************************************
    // permet d'afficher les données ou le borrower est la republique d'albanie
    // ************************************************************************
    //        FindIterable<Document> iterable = collection.find(
    //                new Document("borrower", "REPUBLIC OF ALBANIA"));
    //        iterable.forEach(new Block<Document>() {
    //            @Override
    //            public void apply(final Document document) {
    //                System.out.println(document);
    //            }
    //        });

    // ****************************************************************
    // permet de trouver les marjor secteur energy and mining > 40%
    // ****************************************************************
    //        FindIterable<Document> iterable = collection.find(
    //                new Document("majorsector_percent.Name", "Energy and
    // mining").append("majorsector_percent.Percent",new Document("$lt", 40)));
    //        iterable.forEach(new Block<Document>() {
    //            @Override
    //            public void apply(final Document document) {
    //                System.out.println(document);
    //            }
    //        });
    // *****************************************************
    // permet d'ouvrir l'interface
    // ****************************************************/
    MainWindow page = new MainWindow();
  }
Exemple #2
0
  public static void main(String[] args) throws UnknownHostException {
    MongoClient client = new MongoClient();
    MongoDatabase mongoDatabase = client.getDatabase("course");
    MongoCollection<Document> collection = mongoDatabase.getCollection("findTest");

    collection.drop();

    // insert 10 documents with a random integer as the value of field "x"
    for (int i = 0; i < 10; i++) {
      collection.insertOne(new Document("x", new Random().nextInt(100)));
    }

    System.out.println("Find one:");
    Document one = collection.find().first();
    System.out.println(one);

    System.out.println("\nFind all: ");
    List<Document> all = collection.find().into(new ArrayList<Document>());
    System.out.println(all);

    System.out.println("\nFind all cursor: ");
    MongoCursor<Document> cursor = collection.find().iterator();
    try {

      while (cursor.hasNext()) {
        System.out.println(cursor.next());
      }

    } finally {
      cursor.close();
    }

    //        System.out.println("\nFind all: ");
    //        DBCursor cursor = collection.find();
    //        try {
    //          while (cursor.hasNext()) {
    //              DBObject cur = cursor.next();
    //              System.out.println(cur);
    //          }
    //        } finally {
    //            cursor.close();
    //        }
    //
    System.out.println("\nCount:");
    long count = collection.count();
    System.out.println(count);
  }