/
ReactiveStreamsMongoCollections.kt
1335 lines (1246 loc) · 48.4 KB
/
ReactiveStreamsMongoCollections.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2016/2022 Litote
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.litote.kmongo.reactor
import com.mongodb.bulk.BulkWriteResult
import com.mongodb.client.model.BulkWriteOptions
import com.mongodb.client.model.CountOptions
import com.mongodb.client.model.DeleteOptions
import com.mongodb.client.model.FindOneAndDeleteOptions
import com.mongodb.client.model.FindOneAndReplaceOptions
import com.mongodb.client.model.FindOneAndUpdateOptions
import com.mongodb.client.model.IndexOptions
import com.mongodb.client.model.InsertOneOptions
import com.mongodb.client.model.ReplaceOptions
import com.mongodb.client.model.UpdateOptions
import com.mongodb.client.model.WriteModel
import com.mongodb.client.result.DeleteResult
import com.mongodb.client.result.UpdateResult
import com.mongodb.reactivestreams.client.ClientSession
import com.mongodb.reactivestreams.client.MongoCollection
import com.mongodb.reactor.client.AggregateFlux
import com.mongodb.reactor.client.DistinctFlux
import com.mongodb.reactor.client.FindFlux
import com.mongodb.reactor.client.ListIndexesFlux
import com.mongodb.reactor.client.MapReduceFlux
import com.mongodb.reactor.client.ReactorMongoCollection
import com.mongodb.reactor.client.toReactor
import org.bson.BsonDocument
import org.bson.conversions.Bson
import org.litote.kmongo.EMPTY_BSON
import org.litote.kmongo.SetTo
import org.litote.kmongo.and
import org.litote.kmongo.ascending
import org.litote.kmongo.path
import org.litote.kmongo.set
import org.litote.kmongo.util.KMongoUtil
import org.litote.kmongo.util.KMongoUtil.filterIdToBson
import org.litote.kmongo.util.KMongoUtil.idFilterQuery
import org.litote.kmongo.util.KMongoUtil.toBson
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty1
/**
* Create a new MongoCollection instance with a different default class to cast any documents returned from the database into..
*
* @param <NewTDocument> the default class to cast any documents returned from the database into.
* @return a new MongoCollection instance with the different default class
*/
inline fun <reified NewTDocument : Any> MongoCollection<*>.withDocumentClass(): ReactorMongoCollection<NewTDocument> {
return toReactor().withDocumentClass(NewTDocument::class.java)
}
/**
* Counts the number of documents in the collection according to the given options.
*
* @param filter the query filter
* @return count of filtered collection
*/
fun <T> MongoCollection<T>.countDocuments(filter: String, options: CountOptions = CountOptions()): Mono<Long> {
return countDocuments(toBson(filter), options).toMono()
}
/**
* Counts the number of documents in the collection according to the given options.
*
* @param clientSession the client session
* @param filter the query filter
* @return count of filtered collection
*/
fun <T> MongoCollection<T>.countDocuments(
clientSession: ClientSession,
filter: String,
options: CountOptions = CountOptions()
): Mono<Long> {
return countDocuments(clientSession, toBson(filter), options).toMono()
}
/**
* Gets the distinct values of the specified field name.
*
* @param fieldName the field name
* @param <TResult> the target type of the iterable
* @return an iterable of distinct values
*/
inline fun <reified TResult : Any> MongoCollection<*>.distinct(fieldName: String): DistinctFlux<TResult> {
return distinct(fieldName, KMongoUtil.EMPTY_JSON)
}
/**
* Gets the distinct values of the specified field name.
*
* @param clientSession the client session
* @param fieldName the field name
* @param <TResult> the target type of the iterable
* @return an iterable of distinct values
*/
inline fun <reified TResult : Any> MongoCollection<*>.distinct(
clientSession: ClientSession,
fieldName: String
): DistinctFlux<TResult> {
return distinct(clientSession, fieldName, KMongoUtil.EMPTY_JSON)
}
/**
* Gets the distinct values of the specified field name.
*
* @param fieldName the field name
* @param filter the query filter
* @param <TResult> the target type of the iterable
* @return an iterable of distinct values
*/
inline fun <reified TResult : Any> MongoCollection<*>.distinct(
fieldName: String,
filter: String
): DistinctFlux<TResult> {
return toReactor().distinct(fieldName, toBson(filter), TResult::class.java)
}
/**
* Gets the distinct values of the specified field name.
*
* @param clientSession the client session
* @param fieldName the field name
* @param filter the query filter
* @param <TResult> the target type of the iterable
* @return an iterable of distinct values
*/
inline fun <reified TResult : Any> MongoCollection<*>.distinct(
clientSession: ClientSession,
fieldName: String,
filter: String
): DistinctFlux<TResult> {
return toReactor().distinct(clientSession, fieldName, toBson(filter), TResult::class.java)
}
/**
* Gets the distinct values of the specified field name.
*
* @param field the field name
* @param filter the query filter
* @param <TResult> the target type of the iterable
* @return an iterable of distinct values
*/
inline fun <reified T : Any, reified TResult : Any> MongoCollection<*>.distinct(
field: KProperty1<T, TResult>,
filter: Bson = EMPTY_BSON
): DistinctFlux<TResult> {
return toReactor().distinct(field.path(), filter, TResult::class.java)
}
/**
* Gets the distinct values of the specified field name.
*
* @param clientSession the client session
* @param field the field name
* @param filter the query filter
* @param <TResult> the target type of the iterable
* @return an iterable of distinct values
*/
inline fun <reified T : Any, reified TResult : Any> MongoCollection<*>.distinct(
clientSession: ClientSession,
field: KProperty1<T, TResult>,
filter: Bson = EMPTY_BSON
): DistinctFlux<TResult> {
return toReactor().distinct(clientSession, field.path(), filter, TResult::class.java)
}
/**
* Finds all documents that match the filter in the collection.
*
* @param filter the query filter
* @return the find iterable interface
*/
fun <T : Any> MongoCollection<T>.find(filter: String): FindFlux<T> = toReactor().find(toBson(filter))
/**
* Finds all documents that match the filter in the collection.
*
* @param clientSession the client session
* @param filter the query filter
* @return the find iterable interface
*/
fun <T : Any> MongoCollection<T>.find(clientSession: ClientSession, filter: String): FindFlux<T> {
return toReactor().find(clientSession, toBson(filter))
}
/**
* Finds all documents that match the filters in the collection.
*
* @param filters the query filter
* @return the find iterable interface
*/
fun <T : Any> MongoCollection<T>.find(vararg filters: Bson?): FindFlux<T> = toReactor().find(and(*filters))
/**
* Finds all documents that match the filters in the collection.
*
* @param clientSession the client session
* @param filters the query filter
* @return the find iterable interface
*/
fun <T : Any> MongoCollection<T>.find(clientSession: ClientSession, vararg filters: Bson?): FindFlux<T> {
return toReactor().find(clientSession, and(*filters))
}
/**
* Finds the first document that match the filter in the collection.
*
* @param filter the query filter
*/
fun <T : Any> MongoCollection<T>.findOne(filter: String = KMongoUtil.EMPTY_JSON): Mono<T> = find(filter).toMono()
/**
* Finds the first document that match the filter in the collection.
*
* @param clientSession the client session
* @param filter the query filter
*/
fun <T : Any> MongoCollection<T>.findOne(clientSession: ClientSession, filter: String = KMongoUtil.EMPTY_JSON): Mono<T> {
return find(clientSession, filter).toMono()
}
/**
* Finds the first document that match the filter in the collection.
*
* @param filter the query filter
*/
fun <T : Any> MongoCollection<T>.findOne(filter: Bson): Mono<T> = find(filter).first().toMono()
/**
* Finds the first document that match the filter in the collection.
*
* @param clientSession the client session
* @param filter the query filter
*/
fun <T : Any> MongoCollection<T>.findOne(clientSession: ClientSession, filter: Bson): Mono<T> = find(clientSession, filter).first().toMono()
/**
* Finds the first document that match the filters in the collection.
*
* @param filters the query filters
*/
fun <T : Any> MongoCollection<T>.findOne(vararg filters: Bson?): Mono<T> = find(*filters).first().toMono()
/**
* Finds the first document that match the filters in the collection.
*
* @param clientSession the client session
* @param filters the query filters
*/
fun <T : Any> MongoCollection<T>.findOne(clientSession: ClientSession, vararg filters: Bson?): Mono<T> {
return find(clientSession, *filters).first().toMono()
}
/**
* Finds the document that match the id parameter.
*
* @param id the object id
*/
fun <T : Any> MongoCollection<T>.findOneById(id: Any): Mono<T> {
return findOne(idFilterQuery(id))
}
/**
* Finds the document that match the id parameter.
*
* @param clientSession the client session
* @param id the object id
*/
fun <T : Any> MongoCollection<T>.findOneById(clientSession: ClientSession, id: Any): Mono<T> {
return findOne(clientSession, idFilterQuery(id))
}
/**
* Aggregates documents according to the specified aggregation pipeline. If the pipeline ends with a $out stage, the returned
* iterable will be a query of the collection that the aggregation was written to. Note that in this case the pipeline will be
* executed even if the iterable is never iterated.
*
* @param pipeline the aggregate pipeline
* @param <TResult> the target document type of the iterable
* @return an iterable containing the result of the aggregation operation
*/
inline fun <reified TResult : Any> MongoCollection<*>.aggregate(vararg pipeline: String): AggregateFlux<TResult> {
return toReactor().aggregate(KMongoUtil.toBsonList(pipeline, codecRegistry), TResult::class.java)
}
/**
* Aggregates documents according to the specified aggregation pipeline. If the pipeline ends with a $out stage, the returned
* iterable will be a query of the collection that the aggregation was written to. Note that in this case the pipeline will be
* executed even if the iterable is never iterated.
*
* @param clientSession the client session
* @param pipeline the aggregate pipeline
* @param <TResult> the target document type of the iterable
* @return an iterable containing the result of the aggregation operation
*/
inline fun <reified TResult : Any> MongoCollection<*>.aggregate(
clientSession: ClientSession,
vararg pipeline: String
): AggregateFlux<TResult> {
return toReactor().aggregate(clientSession, KMongoUtil.toBsonList(pipeline, codecRegistry), TResult::class.java)
}
/**
* Aggregates documents according to the specified aggregation pipeline. If the pipeline ends with a $out stage, the returned
* iterable will be a query of the collection that the aggregation was written to. Note that in this case the pipeline will be
* executed even if the iterable is never iterated.
*
* @param pipeline the aggregate pipeline
* @param <TResult> the target document type of the iterable
* @return an iterable containing the result of the aggregation operation
*/
inline fun <reified TResult : Any> MongoCollection<*>.aggregate(vararg pipeline: Bson): AggregateFlux<TResult> {
return toReactor().aggregate(pipeline.toList(), TResult::class.java)
}
/**
* Aggregates documents according to the specified aggregation pipeline. If the pipeline ends with a $out stage, the returned
* iterable will be a query of the collection that the aggregation was written to. Note that in this case the pipeline will be
* executed even if the iterable is never iterated.
*
* @param clientSession the client session
* @param pipeline the aggregate pipeline
* @param <TResult> the target document type of the iterable
* @return an iterable containing the result of the aggregation operation
*/
inline fun <reified TResult : Any> MongoCollection<*>.aggregate(
clientSession: ClientSession,
vararg pipeline: Bson
): AggregateFlux<TResult> {
return toReactor().aggregate(clientSession, pipeline.toList(), TResult::class.java)
}
/**
* Aggregates documents according to the specified map-reduce function.
*
* @param mapFunction a JavaScript function that associates or "maps" a value with a key and emits the key and value pair.
* @param reduceFunction a JavaScript function that "reduces" to a single object all the values associated with a particular key.
* @param <TResult> the target document type of the iterable.
* *
* @return an iterable containing the result of the map-reduce operation
*/
inline fun <reified TResult : Any> MongoCollection<*>.mapReduceTyped(
mapFunction: String,
reduceFunction: String
): MapReduceFlux<TResult> {
return toReactor().mapReduce(mapFunction, reduceFunction, TResult::class.java)
}
/**
* Aggregates documents according to the specified map-reduce function.
*
* @param clientSession the client session
* @param mapFunction a JavaScript function that associates or "maps" a value with a key and emits the key and value pair.
* @param reduceFunction a JavaScript function that "reduces" to a single object all the values associated with a particular key.
* @param <TResult> the target document type of the iterable.
* *
* @return an iterable containing the result of the map-reduce operation
*/
inline fun <reified TResult : Any> MongoCollection<*>.mapReduceTyped(
clientSession: ClientSession,
mapFunction: String,
reduceFunction: String
): MapReduceFlux<TResult> {
return toReactor().mapReduce(clientSession, mapFunction, reduceFunction, TResult::class.java)
}
/**
* Inserts the provided object. If the document is missing an identifier, the driver should generate one.
*
* @param document the document to insert
* @param options the options to apply to the operation
*/
inline fun <reified T : Any> MongoCollection<T>.insert(document: T, options: InsertOneOptions = InsertOneOptions()): Mono<Void> {
return insertOne(document, options)
.toMono()
.then()
}
/**
* Inserts the provided document. If the document is missing an identifier, the driver should generate one.
*
* @param document the document to insert
* @param options the options to apply to the operation
*/
inline fun <reified T : Any> MongoCollection<T>.insertOne(document: String, options: InsertOneOptions = InsertOneOptions()): Mono<Void> {
return withDocumentClass<BsonDocument>().insertOne(toBson(document, T::class), options)
.toMono()
.then()
}
/**
* Inserts the provided document. If the document is missing an identifier, the driver should generate one.
* @param clientSession the client session
* @param document the document to insert
* @param options the options to apply to the operation
*/
inline fun <reified T : Any> MongoCollection<T>.insertOne(
clientSession: ClientSession,
document: String,
options: InsertOneOptions = InsertOneOptions()
): Mono<Void> {
return withDocumentClass<BsonDocument>().insertOne(clientSession, toBson(document, T::class), options)
.toMono()
.then()
}
/**
* Removes at most one document from the collection that matches the given filter. If no documents match, the collection is not
* modified.
*
* @param filter the query filter to apply the the delete operation
*
* @return the result of the remove one operation
*/
fun <T> MongoCollection<T>.deleteOne(filter: String): Mono<DeleteResult> = deleteOne(toBson(filter)).toMono()
/**
* Removes at most one document from the collection that matches the given filter. If no documents match, the collection is not
* modified.
*
* @param clientSession the client session
* @param filter the query filter to apply the the delete operation
*
* @return the result of the remove one operation
*/
fun <T> MongoCollection<T>.deleteOne(clientSession: ClientSession, filter: String): Mono<DeleteResult> {
return deleteOne(clientSession, toBson(filter)).toMono()
}
/**
* Removes at most one document from the collection that matches the given filter. If no documents match, the collection is not
* modified.
*
* @param filters the query filter to apply the the delete operation
*
* @return the result of the remove one operation
*
* @throws com.mongodb.MongoWriteException
* @throws com.mongodb.MongoWriteConcernException
* @throws com.mongodb.MongoException
*/
fun <T> MongoCollection<T>.deleteOne(vararg filters: Bson?): Mono<DeleteResult> = deleteOne(and(*filters)).toMono()
/**
* Removes at most one document from the collection that matches the given filter. If no documents match, the collection is not
* modified.
*
* @param clientSession the client session
* @param filters the query filter to apply the the delete operation
*
* @return the result of the remove one operation
*
* @throws com.mongodb.MongoWriteException
* @throws com.mongodb.MongoWriteConcernException
* @throws com.mongodb.MongoException
*/
fun <T> MongoCollection<T>.deleteOne(clientSession: ClientSession, vararg filters: Bson?): Mono<DeleteResult> {
return deleteOne(clientSession, and(*filters)).toMono()
}
/**
* Removes at most one document from the id parameter. If no documents match, the collection is not
* modified.
*
* @param id the object id
*/
fun <T> MongoCollection<T>.deleteOneById(id: Any): Mono<DeleteResult> = deleteOne(idFilterQuery(id)).toMono()
/**
* Removes at most one document from the id parameter. If no documents match, the collection is not
* modified.
*
* @param clientSession the client session
* @param id the object id
*/
fun <T> MongoCollection<T>.deleteOneById(clientSession: ClientSession, id: Any): Mono<DeleteResult> {
return deleteOne(clientSession, idFilterQuery(id)).toMono()
}
/**
* Removes all documents from the collection that match the given query filter. If no documents match, the collection is not modified.
*
* @param filter the query filter to apply the the delete operation
* @param options the options to apply to the delete operation
*/
fun <T> MongoCollection<T>.deleteMany(filter: String, options: DeleteOptions = DeleteOptions()): Mono<DeleteResult> {
return deleteMany(toBson(filter), options).toMono()
}
/**
* Removes all documents from the collection that match the given query filter. If no documents match, the collection is not modified.
*
* @param clientSession the client session
* @param filter the query filter to apply the the delete operation
* @param options the options to apply to the delete operation
*/
fun <T> MongoCollection<T>.deleteMany(
clientSession: ClientSession,
filter: String,
options: DeleteOptions = DeleteOptions()
): Mono<DeleteResult> {
return deleteMany(clientSession, toBson(filter), options).toMono()
}
/**
* Removes all documents from the collection that match the given query filter. If no documents match, the collection is not modified.
*
* @param filters the query filters to apply the the delete operation
* @param options the options to apply to the delete operation
*/
fun <T> MongoCollection<T>.deleteMany(vararg filters: Bson?, options: DeleteOptions = DeleteOptions()): Mono<DeleteResult> {
return deleteMany(and(*filters), options).toMono()
}
/**
* Removes all documents from the collection that match the given query filter. If no documents match, the collection is not modified.
*
* @param clientSession the client session
* @param filters the query filters to apply the the delete operation
* @param options the options to apply to the delete operation
*/
fun <T> MongoCollection<T>.deleteMany(
clientSession: ClientSession,
vararg filters: Bson?,
options: DeleteOptions = DeleteOptions()
): Mono<DeleteResult> {
return deleteMany(clientSession, and(*filters), options).toMono()
}
/**
* Save the document.
* If the document has no id field, or if the document has a null id value, insert the document.
* Otherwise, call [replaceOneById] with upsert true.
*
* @param document the document to save
*/
fun <T : Any> MongoCollection<T>.save(document: T): Mono<Void> {
val id = KMongoUtil.getIdValue(document)
return if (id != null) {
replaceOneById(id, document, ReplaceOptions().upsert(true)).then()
} else {
insertOne(document).toMono().then()
}
}
/**
* Save the document.
* If the document has no id field, or if the document has a null id value, insert the document.
* Otherwise, call [replaceOneById] with upsert true.
*
* @param clientSession the client session
* @param document the document to save
*/
fun <T : Any> MongoCollection<T>.save(clientSession: ClientSession, document: T): Mono<Void> {
val id = KMongoUtil.getIdValue(document)
return if (id != null) {
replaceOneById(clientSession, id, document, ReplaceOptions().upsert(true)).then()
} else {
insertOne(clientSession, document).toMono().then()
}
}
/**
* Replace a document in the collection according to the specified arguments.
*
* @param id the object id
* @param replacement the replacement document
* @param options the options to apply to the replace operation
*
* @return the result of the replace one operation
*/
fun <T : Any> MongoCollection<T>.replaceOneById(id: Any, replacement: T, options: ReplaceOptions = ReplaceOptions()): Mono<UpdateResult> {
return withDocumentClass<BsonDocument>().replaceOne(idFilterQuery(id), filterIdToBson(replacement), options).toMono()
}
/**
* Replace a document in the collection according to the specified arguments.
*
* @param clientSession the client session
* @param id the object id
* @param replacement the replacement document
* @param options the options to apply to the replace operation
*
* @return the result of the replace one operation
*/
fun <T : Any> MongoCollection<T>.replaceOneById(
clientSession: ClientSession,
id: Any,
replacement: T,
options: ReplaceOptions = ReplaceOptions()
): Mono<UpdateResult> {
return withDocumentClass<BsonDocument>().replaceOne(clientSession, idFilterQuery(id), filterIdToBson(replacement), options).toMono()
}
/**
* Replace a document in the collection according to the specified arguments.
*
* @param replacement the document to replace - must have an non null id
* @param options the options to apply to the replace operation
*/
inline fun <reified T : Any> MongoCollection<T>.replaceOne(replacement: T, options: ReplaceOptions = ReplaceOptions()): Mono<UpdateResult> {
return replaceOneById(KMongoUtil.extractId(replacement, T::class), replacement, options)
}
/**
* Replace a document in the collection according to the specified arguments.
*
* @param clientSession the client session
* @param replacement the document to replace - must have an non null id
* @param options the options to apply to the replace operation
*/
inline fun <reified T : Any> MongoCollection<T>.replaceOne(
clientSession: ClientSession,
replacement: T,
options: ReplaceOptions = ReplaceOptions()
): Mono<UpdateResult> {
return replaceOneById(clientSession, KMongoUtil.extractId(replacement, T::class), replacement, options)
}
/**
* Replace a document in the collection according to the specified arguments.
*
* @param filter the query filter to apply to the replace operation
* @param replacement the replacement document
* @param options the options to apply to the replace operation
*
* @return the result of the update one operation
*/
fun <T : Any> MongoCollection<T>.replaceOne(
filter: String,
replacement: T,
options: ReplaceOptions = ReplaceOptions()
): Mono<UpdateResult> {
return replaceOne(toBson(filter), replacement, options).toMono()
}
/**
* Replace a document in the collection according to the specified arguments.
*
* @param clientSession the client session
* @param filter the query filter to apply to the replace operation
* @param replacement the replacement document
* @param options the options to apply to the replace operation
*
* @return the result of the update one operation
*/
fun <T : Any> MongoCollection<T>.replaceOne(
clientSession: ClientSession,
filter: String,
replacement: T,
options: ReplaceOptions = ReplaceOptions()
): Mono<UpdateResult> {
return replaceOne(clientSession, toBson(filter), replacement, options).toMono()
}
/**
* Replace a document in the collection according to the specified arguments.
* The id of the provided document is not used, in order to avoid updated id error.
* You may have to use [UpdateResult.getUpsertedId] in order to retrieve the generated id.
*
* @param filter the query filter to apply to the replace operation
* @param replacement the replacement document
* @param options the options to apply to the replace operation
*
* @return the result of the update one operation
*/
fun <T : Any> MongoCollection<T>.replaceOneWithoutId(
filter: Bson,
replacement: T,
options: ReplaceOptions = ReplaceOptions()
): Mono<UpdateResult> {
return withDocumentClass<BsonDocument>().replaceOne(filter, filterIdToBson(replacement), options)
.toMono()
}
/**
* Replace a document in the collection according to the specified arguments.
* The id of the provided document is not used, in order to avoid updated id error.
* You may have to use [UpdateResult.getUpsertedId] in order to retrieve the generated id.
*
* @param clientSession the client session
* @param filter the query filter to apply to the replace operation
* @param replacement the replacement document
* @param options the options to apply to the replace operation
*
* @return the result of the update one operation
*/
fun <T : Any> MongoCollection<T>.replaceOneWithoutId(
clientSession: ClientSession,
filter: Bson,
replacement: T,
options: ReplaceOptions = ReplaceOptions()
): Mono<UpdateResult> {
return withDocumentClass<BsonDocument>().replaceOne(clientSession, filter, filterIdToBson(replacement), options)
.toMono()
}
/**
* Update a single document in the collection according to the specified arguments.
*
* @param filter a document describing the query filter
* @param update a document describing the update. The update to apply must include only update operators.
* @param options the options to apply to the update operation
*
* @return the result of the update one operation
*/
fun <T> MongoCollection<T>.updateOne(filter: String, update: String, options: UpdateOptions = UpdateOptions()): Mono<UpdateResult> {
return updateOne(toBson(filter), toBson(update), options).toMono()
}
/**
* Update a single document in the collection according to the specified arguments.
*
* @param clientSession the client session
* @param filter a document describing the query filter
* @param update a document describing the update. The update to apply must include only update operators.
* @param options the options to apply to the update operation
*
* @return the result of the update one operation
*/
fun <T> MongoCollection<T>.updateOne(
clientSession: ClientSession,
filter: String,
update: String,
options: UpdateOptions = UpdateOptions()
): Mono<UpdateResult> = updateOne(clientSession, toBson(filter), toBson(update), options).toMono()
/**
* Update a single document in the collection according to the specified arguments.
*
* @param filter a document describing the query filter
* @param target the update object - must have an non null id
* @param options the options to apply to the update operation
*
* @return the result of the update one operation
*/
fun <T : Any> MongoCollection<T>.updateOne(filter: String, target: T, options: UpdateOptions = UpdateOptions()): Mono<UpdateResult> {
return updateOne(toBson(filter), KMongoUtil.toBsonModifier(target), options).toMono()
}
/**
* Update a single document in the collection according to the specified arguments.
*
* @param clientSession the client session
* @param filter a document describing the query filter
* @param target the update object - must have an non null id
* @param options the options to apply to the update operation
*
* @return the result of the update one operation
*/
fun <T : Any> MongoCollection<T>.updateOne(
clientSession: ClientSession,
filter: String,
target: T,
options: UpdateOptions = UpdateOptions()
): Mono<UpdateResult> {
return updateOne(clientSession, toBson(filter), KMongoUtil.toBsonModifier(target), options).toMono()
}
/**
* Update a single document in the collection according to the specified arguments.
*
* @param filter a document describing the query filter
* @param target the update object - must have an non null id
* @param options the options to apply to the update operation
*
* @return the result of the update one operation
*/
fun <T : Any> MongoCollection<T>.updateOne(
filter: Bson,
target: T,
options: UpdateOptions = UpdateOptions()
): Mono<UpdateResult> {
return updateOne(filter, KMongoUtil.toBsonModifier(target), options).toMono()
}
/**
* Update a single document in the collection according to the specified arguments.
*
* @param clientSession the client session
* @param filter a document describing the query filter
* @param target the update object - must have an non null id
* @param options the options to apply to the update operation
*
* @return the result of the update one operation
*/
fun <T : Any> MongoCollection<T>.updateOne(
clientSession: ClientSession,
filter: Bson,
target: T,
options: UpdateOptions = UpdateOptions()
): Mono<UpdateResult> {
return updateOne(clientSession, filter, KMongoUtil.toBsonModifier(target), options).toMono()
}
/**
* Update a single document in the collection according to the specified arguments.
*
* @param target the update object - must have an non null id
* @param options the options to apply to the update operation
*
* @return the result of the update one operation
*/
inline fun <reified T : Any> MongoCollection<T>.updateOne(target: T, options: UpdateOptions = UpdateOptions()): Mono<UpdateResult> {
return updateOneById(KMongoUtil.extractId(target, T::class), target, options)
}
/**
* Update a single document in the collection according to the specified arguments.
*
* @param clientSession the client session
* @param target the update object - must have an non null id
* @param options the options to apply to the update operation
*
* @return the result of the update one operation
*/
inline fun <reified T : Any> MongoCollection<T>.updateOne(
clientSession: ClientSession,
target: T,
options: UpdateOptions = UpdateOptions()
): Mono<UpdateResult> {
return updateOneById(clientSession, KMongoUtil.extractId(target, T::class), target, options)
}
/**
* Update a single document in the collection according to the specified arguments.
*
* @param id the object id
* @param update the update object
* @param options the options to apply to the update operation
*
* @return the result of the update one operation
*/
fun <T> MongoCollection<T>.updateOneById(
id: Any,
update: Any,
options: UpdateOptions = UpdateOptions()
): Mono<UpdateResult> {
return updateOne(idFilterQuery(id), KMongoUtil.toBsonModifier(update), options)
.toMono()
}
/**
* Update a single document in the collection according to the specified arguments.
*
* @param clientSession the client session
* @param id the object id
* @param update the update object
* @param options the options to apply to the update operation
*
* @return the result of the update one operation
*/
fun <T> MongoCollection<T>.updateOneById(
clientSession: ClientSession,
id: Any,
update: Any,
options: UpdateOptions = UpdateOptions()
): Mono<UpdateResult> {
return updateOne(clientSession, idFilterQuery(id), KMongoUtil.toBsonModifier(update), options)
.toMono()
}
/**
* Update all documents in the collection according to the specified arguments.
*
* @param filter a document describing the query filter
* @param update a document describing the update. The update to apply must include only update operators.
* @param options the options to apply to the update operation
*
* @return the result of the update many operation
*/
fun <T> MongoCollection<T>.updateMany(
filter: String,
update: String,
updateOptions: UpdateOptions = UpdateOptions()
): Mono<UpdateResult> {
return updateMany(toBson(filter), toBson(update), updateOptions).toMono()
}
/**
* Update all documents in the collection according to the specified arguments.
*
* @param clientSession the client session
* @param filter a document describing the query filter
* @param update a document describing the update. The update to apply must include only update operators.
* @param options the options to apply to the update operation
*
* @return the result of the update many operation
*/
fun <T> MongoCollection<T>.updateMany(
clientSession: ClientSession,
filter: String,
update: String,
updateOptions: UpdateOptions = UpdateOptions()
): Mono<UpdateResult> {
return updateMany(clientSession, toBson(filter), toBson(update), updateOptions).toMono()
}
/**
* Update all documents in the collection according to the specified arguments.
*
* @param filter a document describing the query filter
* @param updates a document describing the update. The update to apply must include only update operators.
* @param updateOptions the options to apply to the update operation
*
* @return the result of the update many operation
*/
fun <T> MongoCollection<T>.updateMany(
filter: Bson,
vararg updates: SetTo<*>,
updateOptions: UpdateOptions = UpdateOptions()
): Mono<UpdateResult> {
return updateMany(filter, set(*updates), updateOptions).toMono()
}
/**
* Update all documents in the collection according to the specified arguments.
*
* @param clientSession the client session
* @param filter a document describing the query filter
* @param updates a document describing the update. The update to apply must include only update operators.
* @param updateOptions the options to apply to the update operation
*
* @return the result of the update many operation
*/
fun <T> MongoCollection<T>.updateMany(
clientSession: ClientSession,
filter: Bson,
vararg updates: SetTo<*>,
updateOptions: UpdateOptions = UpdateOptions()
): Mono<UpdateResult> {
return updateMany(clientSession, filter, set(*updates), updateOptions).toMono()
}
/**
* Atomically find a document and remove it.
*
* @param filter the query filter to find the document with
* @param options the options to apply to the operation
*
* @return the document that was removed. If no documents matched the query filter, then null will be returned
*/
fun <T : Any> MongoCollection<T>.findOneAndDelete(
filter: String,
options: FindOneAndDeleteOptions = FindOneAndDeleteOptions()
): Mono<T> {
return findOneAndDelete(toBson(filter), options).toMono()
}
/**
* Atomically find a document and remove it.
*
* @param clientSession the client session
* @param filter the query filter to find the document with
* @param options the options to apply to the operation
*
* @return the document that was removed. If no documents matched the query filter, then null will be returned
*/
fun <T : Any> MongoCollection<T>.findOneAndDelete(
clientSession: ClientSession,
filter: String,
options: FindOneAndDeleteOptions = FindOneAndDeleteOptions()
): Mono<T> {
return findOneAndDelete(clientSession, toBson(filter), options).toMono()
}
/**
* Atomically find a document and replace it.
*
* @param filter the query filter to apply the the replace operation
* @param replacement the replacement document
* @param options the options to apply to the operation
*
* @return the document that was updated. Depending on the value of the `returnOriginal` property, this will either be the
* document as it was before the update or as it is after the update. If no documents matched the query filter, then null will be
* returned
*/
fun <T> MongoCollection<T>.findOneAndReplace(
filter: String,
replacement: T,
options: FindOneAndReplaceOptions = FindOneAndReplaceOptions()
): Mono<T> {
return findOneAndReplace(toBson(filter), replacement, options).toMono()