11package com .start ;
22
3- import com .mongodb .Block ;
4- import com .mongodb .client .MongoClients ;
3+ import ch .qos .logback .classic .Level ;
4+ import ch .qos .logback .classic .Logger ;
5+
6+ import com .mongodb .client .AggregateIterable ;
57import com .mongodb .client .MongoClient ;
8+ import com .mongodb .client .MongoClients ;
69import com .mongodb .client .MongoCollection ;
710import com .mongodb .client .MongoCursor ;
811import com .mongodb .client .MongoDatabase ;
1720import com .mongodb .client .result .DeleteResult ;
1821
1922import org .bson .Document ;
23+ import org .slf4j .LoggerFactory ;
2024
2125import java .util .ArrayList ;
2226import java .util .List ;
2832import static com .mongodb .client .model .Projections .excludeId ;
2933import static com .mongodb .client .model .Sorts .descending ;
3034
35+ @ SuppressWarnings ("ConstantConditions" )
3136public class Getstarted {
3237 public static void main (final String [] args ) {
38+
39+ // See https://www.mongodb.com/docs/drivers/java/sync/current/fundamentals/logging/#logger-names
40+ // for available logger names that can be individually configured, in addition to this default
41+ // that covers all of them (logger names are hierarchical and inherit from their ancestor loggers)
42+ Logger root = (Logger ) LoggerFactory .getLogger ("org.mongodb.driver" );
43+ // Available levels are: OFF, ERROR, WARN, INFO, DEBUG, TRACE, ALL
44+ root .setLevel (Level .WARN );
45+
3346 String mongoURI = System .getenv ("MONGODB_URI" );
3447
3548 MongoClient mongoClient = MongoClients .create (mongoURI );
@@ -53,7 +66,7 @@ public static void main(final String[] args) {
5366 System .out .println ("\t " + myDoc .toJson ());
5467
5568 // insert many
56- List <Document > documents = new ArrayList <Document >();
69+ List <Document > documents = new ArrayList <>();
5770 for (int i = 0 ; i < 5 ; i ++) {
5871 documents .add (new Document ("i" , i ));
5972 }
@@ -62,13 +75,11 @@ public static void main(final String[] args) {
6275 System .out .println ("\t Total # of documents: " + collection .countDocuments ());
6376
6477 // lets get all the documents in the collection and print them out
65- MongoCursor <Document > cursor = collection .find ().iterator ();
66- try {
78+ try ( MongoCursor <Document > cursor = collection .find ().iterator ())
79+ {
6780 while (cursor .hasNext ()) {
6881 System .out .println (cursor .next ().toJson ());
6982 }
70- } finally {
71- cursor .close ();
7283 }
7384
7485 // now use a query to get 1 document out
@@ -99,15 +110,10 @@ public static void main(final String[] args) {
99110 // Aggregation
100111 Document group = Document .parse ("{$group:{_id: null, total :{$sum:'$i'}}}" );
101112 List <Document > pipeline = asList (group );
102-
103- Consumer <Document > printBlock = new Consumer <Document >() {
104- public void accept (final Document doc ) {
105- System .out .println (doc .toJson ());
106- };
107- };
113+
108114 AggregateIterable <Document > iterable = collection .aggregate (pipeline );
109115 System .out .println ("Aggregation Result:" );
110- iterable .forEach (printBlock );
116+ iterable .forEach (documentX -> System . out . println ( documentX . toJson ()) );
111117
112118 // release resources
113119 mongoClient .close ();
0 commit comments