7
7
import com .mongodb .client .FindIterable ;
8
8
import com .mongodb .client .MongoCollection ;
9
9
import com .mongodb .client .MongoDatabase ;
10
+ import com .mongodb .client .MongoCursor ;
10
11
11
12
import static com .mongodb .client .model .Filters .and ;
12
13
import static com .mongodb .client .model .Filters .eq ;
15
16
import static com .mongodb .client .model .Filters .regex ;
16
17
17
18
import static java .util .Collections .singletonList ;
19
+ import static java .util .Arrays .asList ;
20
+
18
21
import com .mongodb .MongoClient ;
19
22
import com .mongodb .MongoClientURI ;
20
23
@@ -28,48 +31,37 @@ public static void main(String args[]) {
28
31
29
32
private static void testCollectionBinding () {
30
33
31
- // final String uriString = "mongodb://testuser:password@localhost:27017/test?authSource=admin";
32
-
33
- // MongoClientURI uri = new MongoClientURI(uriString);
34
- // note that java connections are not initialized unless an operation
35
- // such as a find() or count() is executed
36
-
37
34
// Start Connection
38
- MongoClient mongoClient = Connect .getConnection ();
35
+ final String uriString = "<URISTRING>" ;
36
+ MongoClientURI uri = new MongoClientURI (uriString );
37
+ MongoClient mongoClient = new MongoClient (uri );
39
38
// End Connection
40
- // Start Collection Bind
39
+
40
+ // Start Collection
41
41
MongoDatabase db = mongoClient .getDatabase ("test" );
42
42
MongoCollection <Document > collection = db
43
43
.getCollection ("inventory" );
44
- // End Collection Bind
44
+ // End Collection
45
45
collection .drop ();
46
46
47
47
// Insert Guide test
48
- Document canvas = new Document ("item" , "canvas" )
49
- .append ("qty" , 100 )
50
- .append ("tags" , singletonList ("cotton" ));
51
-
52
- Document size = new Document ("h" , 28 ).append ("w" , 35.5 )
53
- .append ("uom" , "cm" );
54
- canvas .put ("size" , size );
48
+ Document canvas = Document .parse ("{ item: 'canvas', qty: 100, tags: ['cotton'], size: { h: 28, w: 35.5, uom: 'cm' } }" );
55
49
56
50
collection .insertOne (canvas );
57
51
58
52
// Read Guide 1 Find 1
59
53
FindIterable <Document > findIterable = collection
60
54
.find (new Document ());
61
55
62
- Block <Document > printBlock = new Block <Document >() {
63
- @ Override
64
- public void apply (final Document document ) {
65
- System .out .println (document .toJson ());
66
- }
67
- };
68
-
69
56
System .out .println ("READ GUIDE 1: example 1 results" );
70
- findIterable .forEach (printBlock );
71
57
72
- collection .insertMany (java .util .Arrays .asList (Document .parse (
58
+ try (MongoCursor <Document > cursor = collection .find ().iterator ()) {
59
+ while (cursor .hasNext ()) {
60
+ System .out .println (cursor .next ().toJson ());
61
+ }
62
+ }
63
+
64
+ collection .insertMany (asList (Document .parse (
73
65
"{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }" ),
74
66
Document .parse (
75
67
"{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'A' }" ),
@@ -80,56 +72,73 @@ public void apply(final Document document) {
80
72
Document .parse (
81
73
"{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }" )));
82
74
83
- findIterable = collection .find (eq ("status" , "D" ));
84
-
85
75
System .out .println ("READ GUIDE 2: example 1 results" );
86
- findIterable .forEach (printBlock );
87
76
88
- findIterable = collection .find (eq ("size" ,
89
- Document .parse ("{ h: 14, w: 21, uom: 'cm' }" )));
77
+ try (MongoCursor <Document > cursor = collection .find (eq ("status" , "D" )).iterator ()) {
78
+ while (cursor .hasNext ()) {
79
+ System .out .println (cursor .next ().toJson ());
80
+ }
81
+ }
90
82
91
83
System .out .println ("READ GUIDE 2: example 2 results" );
92
84
93
- findIterable .forEach (printBlock );
85
+ try (MongoCursor <Document > cursor = collection .find (eq ("size" ,
86
+ Document .parse ("{ h: 14, w: 21, uom: 'cm' }" ))).iterator ()) {
87
+ while (cursor .hasNext ()) {
88
+ System .out .println (cursor .next ().toJson ());
89
+ }
90
+ }
94
91
95
- findIterable = collection .find (eq ("size.uom" , "in" ));
96
92
97
93
System .out .println ("READ GUIDE 2: example 3 results" );
98
94
99
- findIterable .forEach (printBlock );
100
-
101
- findIterable = collection .find (lt ("size.h" , 15 ));
95
+ try (MongoCursor <Document > cursor = collection .find (eq ("size.uom" , "in" )).iterator ()) {
96
+ while (cursor .hasNext ()) {
97
+ System .out .println (cursor .next ().toJson ());
98
+ }
99
+ }
102
100
103
101
System .out .println ("READ GUIDE 3: example 1 results" );
104
- findIterable .forEach (printBlock );
105
102
106
- findIterable = collection
107
- .find (and (eq ("status" , "A" ), lt ("qty" , 30 )));
103
+ try (MongoCursor <Document > cursor = collection .find (lt ("size.h" , 15 )).iterator ()) {
104
+ while (cursor .hasNext ()) {
105
+ System .out .println (cursor .next ().toJson ());
106
+ }
107
+ }
108
108
109
109
System .out .println ("READ GUIDE 3: example 2 results" );
110
110
111
- findIterable .forEach (printBlock );
112
-
111
+ try (MongoCursor <Document > cursor = collection
112
+ .find (and (eq ("status" , "A" ), lt ("qty" , 30 ))).iterator ()) {
113
+ while (cursor .hasNext ()) {
114
+ System .out .println (cursor .next ().toJson ());
115
+ }
116
+ }
113
117
114
- findIterable = collection .find (or (eq ("status" , "A" ), lt ("qty" , 30 )));
115
118
116
119
System .out .println ("READ GUIDE 3: example 3 results" );
117
120
118
- findIterable .forEach (printBlock );
119
-
121
+ try (MongoCursor <Document > cursor = collection .
122
+ find (or (eq ("status" , "A" ), lt ("qty" , 30 ))).iterator ()) {
123
+ while (cursor .hasNext ()) {
124
+ System .out .println (cursor .next ().toJson ());
125
+ }
126
+ }
120
127
121
128
System .out .println ("READ GUIDE 3: example 4 results" );
122
129
123
- findIterable = collection .find (and (eq ("status" , "A" ),
124
- or (lt ("qty" , 30 ), regex ("item" , "^p" ))));
125
-
126
- findIterable .forEach (printBlock );
130
+ try (MongoCursor <Document > cursor = collection .find (and (eq ("status" , "A" ),
131
+ or (lt ("qty" , 30 ), regex ("item" , "^p" )))).iterator ()){
132
+ while (cursor .hasNext ()) {
133
+ System .out .println (cursor .next ().toJson ());
134
+ }
135
+ }
127
136
128
137
// Start Close
129
- Connect .closeConnection (mongoClient );
130
-
138
+ mongoClient .close ();
131
139
// End Close
132
140
}
133
141
134
142
}
135
143
144
+
0 commit comments