18
18
19
19
import com .mongodb .MongoClientSettings ;
20
20
import com .mongodb .client .AggregateIterable ;
21
+ import com .mongodb .client .FindIterable ;
21
22
import com .mongodb .client .MongoClient ;
22
23
import com .mongodb .client .MongoClients ;
23
24
import com .mongodb .client .MongoCollection ;
36
37
import static com .mongodb .client .model .Accumulators .sum ;
37
38
import static com .mongodb .client .model .Aggregates .match ;
38
39
import static com .mongodb .client .model .Aggregates .project ;
40
+ import static com .mongodb .client .model .Filters .expr ;
39
41
import static com .mongodb .client .model .Projections .computed ;
40
42
import static com .mongodb .client .model .Projections .excludeId ;
41
43
import static com .mongodb .client .model .Projections .fields ;
42
44
import static com .mongodb .client .model .Projections .include ;
43
45
import static com .mongodb .client .model .Sorts .ascending ;
44
- import static com .mongodb .client .model .expressions .Expressions .current ;
45
- import static com .mongodb .client .model .expressions .Expressions .of ;
46
- import static com .mongodb .client .model .expressions .Expressions .ofArray ;
46
+ import static com .mongodb .client .model .expressions .Expressions .*;
47
47
import static org .junit .jupiter .api .Assertions .assertEquals ;
48
48
49
49
class InContextExpressionsFunctionalTest extends AbstractExpressionsFunctionalTest {
@@ -63,7 +63,7 @@ public void tearDown() {
63
63
client .close ();
64
64
}
65
65
66
- private String bsonToString (final Bson project ) {
66
+ private static String bsonToString (final Bson project ) {
67
67
return project .toBsonDocument (Document .class , MongoClientSettings .getDefaultCodecRegistry ()).toString ().replaceAll ("\" " , "'" );
68
68
}
69
69
@@ -74,6 +74,23 @@ private List<Document> aggregate(final Bson... stages) {
74
74
return results ;
75
75
}
76
76
77
+ @ Test
78
+ public void findTest () {
79
+ col .insertMany (Arrays .asList (
80
+ Document .parse ("{_id: 1, x: 0, y: 2}" ),
81
+ Document .parse ("{_id: 2, x: 0, y: 3}" ),
82
+ Document .parse ("{_id: 3, x: 1, y: 3}" )));
83
+
84
+ FindIterable <Document > iterable = col .find (expr (
85
+ current ().getInteger ("x" ).eq (of (1 ))));
86
+ List <Document > results = new ArrayList <>();
87
+ iterable .forEach (r -> results .add (r ));
88
+
89
+ assertEquals (
90
+ Arrays .asList (Document .parse ("{_id: 3, x: 1, y: 3}" )),
91
+ results );
92
+ }
93
+
77
94
@ Test
78
95
public void matchTest () {
79
96
col .insertMany (Arrays .asList (
@@ -82,7 +99,25 @@ public void matchTest() {
82
99
Document .parse ("{_id: 3, x: 1, y: 3}" )));
83
100
84
101
List <Document > results = aggregate (
85
- match (Filters .expr (current ().getInteger ("x" ).eq (of (1 )))));
102
+ match (expr (current ().getInteger ("x" ).eq (of (1 )))));
103
+
104
+ assertEquals (
105
+ Arrays .asList (Document .parse ("{_id: 3, x: 1, y: 3}" )),
106
+ results );
107
+ }
108
+
109
+ @ Test
110
+ public void currentAsMapMatchTest () {
111
+ col .insertMany (Arrays .asList (
112
+ Document .parse ("{_id: 1, x: 0, y: 2}" ),
113
+ Document .parse ("{_id: 2, x: 0, y: 3}" ),
114
+ Document .parse ("{_id: 3, x: 1, y: 3}" )));
115
+
116
+ List <Document > results = aggregate (
117
+ match (expr (Expressions .<NumberExpression >currentAsMap ()
118
+ .entrySet ()
119
+ .map (e -> e .getValue ())
120
+ .sum (v -> v ).eq (of (7 )))));
86
121
87
122
assertEquals (
88
123
Arrays .asList (Document .parse ("{_id: 3, x: 1, y: 3}" )),
@@ -131,8 +166,8 @@ public void projectTest2() {
131
166
132
167
// new, document
133
168
Bson projectDocument = project (fields (computed ("nested" ,
134
- //BsonDocument.parse( "{ x: {$max : ['$x', 4] }}")
135
- of (new Document ( )).setField ("x" , current ().getInteger ("x" ).max (of (4 )))
169
+ // the below is roughly: "{ x: {$max : ['$x', 4] }}"
170
+ of (Document . parse ( "{x: 9}" )).setField ("x" , current ().getInteger ("x" ).max (of (4 )))
136
171
)));
137
172
assertEquals (
138
173
Arrays .asList (Document .parse ("{_id: 0, nested: { x: 4 } }" )),
0 commit comments