1717package example ;
1818
1919import com .mongodb .BasicDBObject ;
20+ import com .mongodb .BulkWriteOperation ;
21+ import com .mongodb .BulkWriteResult ;
22+ import com .mongodb .Cursor ;
2023import com .mongodb .DB ;
2124import com .mongodb .DBCollection ;
2225import com .mongodb .DBCursor ;
2326import com .mongodb .DBObject ;
2427import com .mongodb .MongoClient ;
28+ import com .mongodb .ParallelScanOptions ;
2529
2630import java .net .UnknownHostException ;
2731import java .util .List ;
2832import java .util .Set ;
2933
34+ import static java .util .concurrent .TimeUnit .SECONDS ;
35+
3036/**
31- * The tutorial from http://www .mongodb.org/display/DOCS/Java+Tutorial.
37+ * The tutorial from http://docs .mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/
3238 */
3339public class QuickTour {
3440 // CHECKSTYLE:OFF
@@ -55,30 +61,31 @@ public static void main(final String[] args) throws UnknownHostException {
5561 }
5662
5763 // get a collection object to work with
58- DBCollection testCollection = db .getCollection ("testCollection" );
64+ DBCollection coll = db .getCollection ("testCollection" );
5965
6066 // drop all the data in it
61- testCollection .drop ();
67+ coll .drop ();
6268
6369 // make a document and insert it
64- BasicDBObject doc = new BasicDBObject ("name" , "MongoDB" ).append ("type" , "database" )
65- .append ("count" , 1 )
66- .append ("info" , new BasicDBObject ("x" , 203 ).append ("y" , 102 ));
70+ BasicDBObject doc = new BasicDBObject ("name" , "MongoDB" )
71+ .append ("type" , "database" )
72+ .append ("count" , 1 )
73+ .append ("info" , new BasicDBObject ("x" , 203 ).append ("y" , 102 ));
6774
68- testCollection .insert (doc );
75+ coll .insert (doc );
6976
7077 // get it (since it's the only one in there since we dropped the rest earlier on)
71- DBObject myDoc = testCollection .findOne ();
78+ DBObject myDoc = coll .findOne ();
7279 System .out .println (myDoc );
7380
7481 // now, lets add lots of little documents to the collection so we can explore queries and cursors
7582 for (int i = 0 ; i < 100 ; i ++) {
76- testCollection .insert (new BasicDBObject ().append ("i" , i ));
83+ coll .insert (new BasicDBObject ().append ("i" , i ));
7784 }
78- System .out .println ("total # of documents after inserting 100 small ones (should be 101) " + testCollection .getCount ());
85+ System .out .println ("total # of documents after inserting 100 small ones (should be 101) " + coll .getCount ());
7986
8087 // lets get all the documents in the collection and print them out
81- DBCursor cursor = testCollection .find ();
88+ DBCursor cursor = coll .find ();
8289 try {
8390 while (cursor .hasNext ()) {
8491 System .out .println (cursor .next ());
@@ -89,7 +96,7 @@ public static void main(final String[] args) throws UnknownHostException {
8996
9097 // now use a query to get 1 document out
9198 BasicDBObject query = new BasicDBObject ("i" , 71 );
92- cursor = testCollection .find (query );
99+ cursor = coll .find (query );
93100
94101 try {
95102 while (cursor .hasNext ()) {
@@ -99,9 +106,24 @@ public static void main(final String[] args) throws UnknownHostException {
99106 cursor .close ();
100107 }
101108
109+ // $ Operators are represented as strings
110+ query = new BasicDBObject ("j" , new BasicDBObject ("$ne" , 3 ))
111+ .append ("k" , new BasicDBObject ("$gt" , 10 ));
112+
113+ cursor = coll .find (query );
114+
115+ try {
116+ while (cursor .hasNext ()) {
117+ System .out .println (cursor .next ());
118+ }
119+ } finally {
120+ cursor .close ();
121+ }
122+
102123 // now use a range query to get a larger subset
103- query = new BasicDBObject ("i" , new BasicDBObject ("$gt" , 50 )); // i.e. find all where i > 50
104- cursor = testCollection .find (query );
124+ // find all where i > 50
125+ query = new BasicDBObject ("i" , new BasicDBObject ("$gt" , 50 ));
126+ cursor = coll .find (query );
105127
106128 try {
107129 while (cursor .hasNext ()) {
@@ -112,8 +134,8 @@ public static void main(final String[] args) throws UnknownHostException {
112134 }
113135
114136 // range query with multiple constraints
115- query = new BasicDBObject ("i" , new BasicDBObject ("$gt" , 20 ).append ("$lte" , 30 )); // i.e. 20 < i <= 30
116- cursor = testCollection .find (query );
137+ query = new BasicDBObject ("i" , new BasicDBObject ("$gt" , 20 ).append ("$lte" , 30 ));
138+ cursor = coll .find (query );
117139
118140 try {
119141 while (cursor .hasNext ()) {
@@ -123,30 +145,46 @@ public static void main(final String[] args) throws UnknownHostException {
123145 cursor .close ();
124146 }
125147
126- // create an index on the "i" field
127- testCollection .createIndex (new BasicDBObject ("i" , 1 )); // create index on "i", ascending
128-
129- // list the indexes on the collection
130- List <DBObject > list = testCollection .getIndexInfo ();
131- for (final DBObject o : list ) {
132- System .out .println (o );
148+ // Count all documents in a collection but take a maximum second to do so
149+ coll .find ().maxTime (1 , SECONDS ).count ();
150+
151+ // Bulk operations
152+ BulkWriteOperation builder = coll .initializeOrderedBulkOperation ();
153+ builder .insert (new BasicDBObject ("_id" , 1 ));
154+ builder .insert (new BasicDBObject ("_id" , 2 ));
155+ builder .insert (new BasicDBObject ("_id" , 3 ));
156+
157+ builder .find (new BasicDBObject ("_id" , 1 )).updateOne (new BasicDBObject ("$set" , new BasicDBObject ("x" , 2 )));
158+ builder .find (new BasicDBObject ("_id" , 2 )).removeOne ();
159+ builder .find (new BasicDBObject ("_id" , 3 )).replaceOne (new BasicDBObject ("_id" , 3 ).append ("x" , 4 ));
160+
161+ BulkWriteResult result = builder .execute ();
162+ System .out .println ("Ordered bulk write result : " + result );
163+
164+ // Unordered bulk operation - no guarantee of order of operation
165+ builder = coll .initializeUnorderedBulkOperation ();
166+ builder .find (new BasicDBObject ("_id" , 1 )).removeOne ();
167+ builder .find (new BasicDBObject ("_id" , 2 )).removeOne ();
168+
169+ result = builder .execute ();
170+ System .out .println ("Ordered bulk write result : " + result );
171+
172+ // parallelScan
173+ ParallelScanOptions parallelScanOptions = ParallelScanOptions
174+ .builder ()
175+ .numCursors (3 )
176+ .batchSize (300 )
177+ .build ();
178+
179+ List <Cursor > cursors = coll .parallelScan (parallelScanOptions );
180+ for (Cursor pCursor : cursors ) {
181+ while (pCursor .hasNext ()) {
182+ System .out .println (pCursor .next ());
183+ }
133184 }
134185
135- // See if the last operation had an error
136- System .out .println ("Last error : " + db .getLastError ());
137-
138- // see if any previous operation had an error
139- System .out .println ("Previous error : " + db .getPreviousError ());
140-
141- // force an error
142- db .forceError ();
143-
144- // See if the last operation had an error
145- System .out .println ("Last error : " + db .getLastError ());
146-
147- db .resetError ();
148-
149186 // release resources
187+ db .dropDatabase ();
150188 mongoClient .close ();
151189 }
152190 // CHECKSTYLE:ON
0 commit comments