38
38
import com .mongodb .client .MongoCollection ;
39
39
import com .mongodb .client .MongoCursor ;
40
40
import com .mongodb .client .MongoDatabase ;
41
+ import com .mongodb .client .MongoIterable ;
41
42
import com .mongodb .client .model .BulkWriteOptions ;
42
43
import com .mongodb .client .model .ChangeStreamPreAndPostImagesOptions ;
43
44
import com .mongodb .client .model .ClusteredIndexOptions ;
@@ -169,10 +170,12 @@ OperationResult executeListDatabases(final BsonDocument operation) {
169
170
: client .listDatabases (session , BsonDocument .class );
170
171
171
172
for (Map .Entry <String , BsonValue > cur : arguments .entrySet ()) {
172
- //noinspection SwitchStatementWithTooFewBranches
173
173
switch (cur .getKey ()) {
174
174
case "session" :
175
175
break ;
176
+ case "filter" :
177
+ iterable .filter (cur .getValue ().asDocument ());
178
+ break ;
176
179
default :
177
180
throw new UnsupportedOperationException ("Unsupported argument: " + cur .getKey ());
178
181
}
@@ -182,6 +185,29 @@ OperationResult executeListDatabases(final BsonDocument operation) {
182
185
new BsonArray (iterable .into (new ArrayList <>())));
183
186
}
184
187
188
+ OperationResult executeListDatabaseNames (final BsonDocument operation ) {
189
+ MongoClient client = entities .getClient (operation .getString ("object" ).getValue ());
190
+
191
+ BsonDocument arguments = operation .getDocument ("arguments" , new BsonDocument ());
192
+ ClientSession session = getSession (arguments );
193
+ MongoIterable <String > iterable = session == null
194
+ ? client .listDatabaseNames ()
195
+ : client .listDatabaseNames (session );
196
+
197
+ for (Map .Entry <String , BsonValue > cur : arguments .entrySet ()) {
198
+ //noinspection SwitchStatementWithTooFewBranches
199
+ switch (cur .getKey ()) {
200
+ case "session" :
201
+ break ;
202
+ default :
203
+ throw new UnsupportedOperationException ("Unsupported argument: " + cur .getKey ());
204
+ }
205
+ }
206
+
207
+ return resultOf (() ->
208
+ new BsonArray (iterable .into (new ArrayList <>()).stream ().map (BsonString ::new ).collect (toList ())));
209
+ }
210
+
185
211
OperationResult executeListCollections (final BsonDocument operation ) {
186
212
MongoDatabase database = entities .getDatabase (operation .getString ("object" ).getValue ());
187
213
@@ -209,7 +235,51 @@ OperationResult executeListCollections(final BsonDocument operation) {
209
235
new BsonArray (iterable .into (new ArrayList <>())));
210
236
}
211
237
238
+ OperationResult executeListCollectionNames (final BsonDocument operation ) {
239
+ MongoDatabase database = entities .getDatabase (operation .getString ("object" ).getValue ());
240
+
241
+ BsonDocument arguments = operation .getDocument ("arguments" );
242
+ ClientSession session = getSession (arguments );
243
+ MongoIterable <String > iterable = session == null
244
+ ? database .listCollectionNames ()
245
+ : database .listCollectionNames (session );
246
+ for (Map .Entry <String , BsonValue > cur : arguments .entrySet ()) {
247
+ switch (cur .getKey ()) {
248
+ case "session" :
249
+ break ;
250
+ case "filter" :
251
+ BsonDocument filter = cur .getValue ().asDocument ();
252
+ if (!filter .isEmpty ()) {
253
+ throw new UnsupportedOperationException ("The driver does not support filtering of collection names" );
254
+ }
255
+ break ;
256
+ case "batchSize" :
257
+ iterable .batchSize (cur .getValue ().asNumber ().intValue ());
258
+ break ;
259
+ default :
260
+ throw new UnsupportedOperationException ("Unsupported argument: " + cur .getKey ());
261
+ }
262
+ }
263
+
264
+ return resultOf (() ->
265
+ new BsonArray (iterable .into (new ArrayList <>()).stream ().map (BsonString ::new ).collect (toList ())));
266
+ }
267
+
212
268
OperationResult executeListIndexes (final BsonDocument operation ) {
269
+ ListIndexesIterable <BsonDocument > iterable = createListIndexesIterable (operation );
270
+
271
+ return resultOf (() ->
272
+ new BsonArray (iterable .into (new ArrayList <>())));
273
+ }
274
+
275
+ OperationResult executeListIndexNames (final BsonDocument operation ) {
276
+ ListIndexesIterable <BsonDocument > iterable = createListIndexesIterable (operation );
277
+
278
+ return resultOf (() ->
279
+ new BsonArray (iterable .into (new ArrayList <>()).stream ().map (document -> document .getString ("name" )).collect (toList ())));
280
+ }
281
+
282
+ private ListIndexesIterable <BsonDocument > createListIndexesIterable (final BsonDocument operation ) {
213
283
MongoCollection <BsonDocument > collection = entities .getCollection (operation .getString ("object" ).getValue ());
214
284
BsonDocument arguments = operation .getDocument ("arguments" , new BsonDocument ());
215
285
ClientSession session = getSession (arguments );
@@ -227,9 +297,7 @@ OperationResult executeListIndexes(final BsonDocument operation) {
227
297
throw new UnsupportedOperationException ("Unsupported argument: " + cur .getKey ());
228
298
}
229
299
}
230
-
231
- return resultOf (() ->
232
- new BsonArray (iterable .into (new ArrayList <>())));
300
+ return iterable ;
233
301
}
234
302
235
303
OperationResult executeFind (final BsonDocument operation ) {
@@ -238,6 +306,11 @@ OperationResult executeFind(final BsonDocument operation) {
238
306
new BsonArray (iterable .into (new ArrayList <>())));
239
307
}
240
308
309
+ OperationResult executeFindOne (final BsonDocument operation ) {
310
+ FindIterable <BsonDocument > iterable = createFindIterable (operation );
311
+ return resultOf (iterable ::first );
312
+ }
313
+
241
314
OperationResult createFindCursor (final BsonDocument operation ) {
242
315
FindIterable <BsonDocument > iterable = createFindIterable (operation );
243
316
return resultOf (() -> {
0 commit comments