17
17
package example ;
18
18
19
19
import com .mongodb .BasicDBObject ;
20
+ import com .mongodb .BulkWriteOperation ;
21
+ import com .mongodb .BulkWriteResult ;
22
+ import com .mongodb .Cursor ;
20
23
import com .mongodb .DB ;
21
24
import com .mongodb .DBCollection ;
22
25
import com .mongodb .DBCursor ;
23
26
import com .mongodb .DBObject ;
24
27
import com .mongodb .MongoClient ;
28
+ import com .mongodb .ParallelScanOptions ;
25
29
26
30
import java .net .UnknownHostException ;
27
31
import java .util .List ;
28
32
import java .util .Set ;
29
33
34
+ import static java .util .concurrent .TimeUnit .SECONDS ;
35
+
30
36
/**
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/
32
38
*/
33
39
public class QuickTour {
34
40
// CHECKSTYLE:OFF
@@ -55,30 +61,31 @@ public static void main(final String[] args) throws UnknownHostException {
55
61
}
56
62
57
63
// get a collection object to work with
58
- DBCollection testCollection = db .getCollection ("testCollection" );
64
+ DBCollection coll = db .getCollection ("testCollection" );
59
65
60
66
// drop all the data in it
61
- testCollection .drop ();
67
+ coll .drop ();
62
68
63
69
// 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 ));
67
74
68
- testCollection .insert (doc );
75
+ coll .insert (doc );
69
76
70
77
// 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 ();
72
79
System .out .println (myDoc );
73
80
74
81
// now, lets add lots of little documents to the collection so we can explore queries and cursors
75
82
for (int i = 0 ; i < 100 ; i ++) {
76
- testCollection .insert (new BasicDBObject ().append ("i" , i ));
83
+ coll .insert (new BasicDBObject ().append ("i" , i ));
77
84
}
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 ());
79
86
80
87
// lets get all the documents in the collection and print them out
81
- DBCursor cursor = testCollection .find ();
88
+ DBCursor cursor = coll .find ();
82
89
try {
83
90
while (cursor .hasNext ()) {
84
91
System .out .println (cursor .next ());
@@ -89,7 +96,7 @@ public static void main(final String[] args) throws UnknownHostException {
89
96
90
97
// now use a query to get 1 document out
91
98
BasicDBObject query = new BasicDBObject ("i" , 71 );
92
- cursor = testCollection .find (query );
99
+ cursor = coll .find (query );
93
100
94
101
try {
95
102
while (cursor .hasNext ()) {
@@ -99,9 +106,24 @@ public static void main(final String[] args) throws UnknownHostException {
99
106
cursor .close ();
100
107
}
101
108
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
+
102
123
// 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 );
105
127
106
128
try {
107
129
while (cursor .hasNext ()) {
@@ -112,8 +134,8 @@ public static void main(final String[] args) throws UnknownHostException {
112
134
}
113
135
114
136
// 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 );
117
139
118
140
try {
119
141
while (cursor .hasNext ()) {
@@ -123,30 +145,46 @@ public static void main(final String[] args) throws UnknownHostException {
123
145
cursor .close ();
124
146
}
125
147
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
+ }
133
184
}
134
185
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
-
149
186
// release resources
187
+ db .dropDatabase ();
150
188
mongoClient .close ();
151
189
}
152
190
// CHECKSTYLE:ON
0 commit comments