1+ package io .helidon .data .examples ;
2+
3+ import java .net .UnknownHostException ;
4+
5+ import com .mongodb .MongoClient ;
6+ import com .mongodb .MongoClientURI ;
7+ import com .mongodb .ServerAddress ;
8+
9+ import com .mongodb .client .MongoDatabase ;
10+ import com .mongodb .client .MongoCollection ;
11+
12+ import org .bson .Document ;
13+ import java .util .Arrays ;
14+ import com .mongodb .Block ;
15+
16+ import com .mongodb .client .MongoCursor ;
17+ import static com .mongodb .client .model .Filters .*;
18+ import com .mongodb .client .result .DeleteResult ;
19+ import static com .mongodb .client .model .Updates .*;
20+ import com .mongodb .client .result .UpdateResult ;
21+ import java .util .ArrayList ;
22+ import java .util .List ;
23+
24+ public class MongoDBAccess {
25+ public void quicktest () {
26+ // Create seed data
27+
28+ List <Document > seedData = new ArrayList <Document >();
29+
30+ seedData .add (new Document ("decade" , "1970s" )
31+ .append ("artist" , "Debby Boone" )
32+ .append ("song" , "You Light Up My Life" )
33+ .append ("weeksAtOne" , 10 )
34+ );
35+
36+ seedData .add (new Document ("decade" , "1980s" )
37+ .append ("artist" , "Olivia Newton-John" )
38+ .append ("song" , "Physical" )
39+ .append ("weeksAtOne" , 10 )
40+ );
41+
42+ seedData .add (new Document ("decade" , "1990s" )
43+ .append ("artist" , "Mariah Carey" )
44+ .append ("song" , "One Sweet Day" )
45+ .append ("weeksAtOne" , 16 )
46+ );
47+
48+ // Standard URI format: mongodb://[dbuser:dbpassword@]host:port/dbname
49+
50+ MongoClientURI uri = new MongoClientURI ("mongodb://user:pass@host:port/db" );
51+ MongoClient client = new MongoClient (uri );
52+ MongoDatabase db = client .getDatabase (uri .getDatabase ());
53+
54+ /*
55+ * First we'll add a few songs. Nothing is required to create the
56+ * songs collection; it is created automatically when we insert.
57+ */
58+
59+ MongoCollection <Document > songs = db .getCollection ("songs" );
60+
61+ // Note that the insert method can take either an array or a document.
62+
63+ songs .insertMany (seedData );
64+
65+ /*
66+ * Then we need to give Boyz II Men credit for their contribution to
67+ * the hit "One Sweet Day".
68+ */
69+
70+ Document updateQuery = new Document ("song" , "One Sweet Day" );
71+ songs .updateOne (updateQuery , new Document ("$set" , new Document ("artist" , "Mariah Carey ft. Boyz II Men" )));
72+
73+ /*
74+ * Finally we run a query which returns all the hits that spent 10
75+ * or more weeks at number 1.
76+ */
77+
78+ Document findQuery = new Document ("weeksAtOne" , new Document ("$gte" ,10 ));
79+ Document orderBy = new Document ("decade" , 1 );
80+
81+ MongoCursor <Document > cursor = songs .find (findQuery ).sort (orderBy ).iterator ();
82+
83+ try {
84+ while (cursor .hasNext ()) {
85+ Document doc = cursor .next ();
86+ System .out .println (
87+ "In the " + doc .get ("decade" ) + ", " + doc .get ("song" ) +
88+ " by " + doc .get ("artist" ) + " topped the charts for " +
89+ doc .get ("weeksAtOne" ) + " straight weeks."
90+ );
91+ }
92+ } finally {
93+ cursor .close ();
94+ }
95+
96+ // Since this is an example, we'll clean up after ourselves.
97+
98+ songs .drop ();
99+
100+ // Only close the connection when your app is terminating
101+
102+ client .close ();
103+ }
104+ }
0 commit comments