-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
UpdateBuilder.cs
3111 lines (2865 loc) · 143 KB
/
UpdateBuilder.cs
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 2010-2014 MongoDB Inc.
*
* 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.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Linq.Utils;
namespace MongoDB.Driver.Builders
{
/// <summary>
/// A builder for creating update modifiers.
/// </summary>
public static class Update
{
// public static methods
/// <summary>
/// Adds a value to a named array element if the value is not already in the array (see $addToSet).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="value">The value to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder AddToSet(string name, BsonValue value)
{
return new UpdateBuilder().AddToSet(name, value);
}
/// <summary>
/// Adds a list of values to a named array element adding each value only if it not already in the array (see $addToSet and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder AddToSetEach(string name, BsonArray values)
{
return new UpdateBuilder().AddToSetEach(name, values);
}
/// <summary>
/// Adds a list of values to a named array element adding each value only if it not already in the array (see $addToSet and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder AddToSetEach(string name, IEnumerable<BsonValue> values)
{
return new UpdateBuilder().AddToSetEach(name, values);
}
/// <summary>
/// Adds a list of values to a named array element adding each value only if it not already in the array (see $addToSet and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder AddToSetEach(string name, params BsonValue[] values)
{
return new UpdateBuilder().AddToSetEach(name, values);
}
/// <summary>
/// Adds a list of wrapped values to a named array element adding each value only if it not already in the array (see $addToSet and $each).
/// </summary>
/// <typeparam name="T">The type of wrapped values.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The wrapped values to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder AddToSetEachWrapped<T>(string name, IEnumerable<T> values)
{
return new UpdateBuilder().AddToSetEachWrapped<T>(name, values);
}
/// <summary>
/// Adds a list of wrapped values to a named array element adding each value only if it not already in the array (see $addToSet and $each).
/// </summary>
/// <typeparam name="T">The type of wrapped values.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The wrapped values to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder AddToSetEachWrapped<T>(string name, params T[] values)
{
return new UpdateBuilder().AddToSetEachWrapped<T>(name, values);
}
/// <summary>
/// Adds a wrapped value to a named array element if the value is not already in the array (see $addToSet).
/// </summary>
/// <typeparam name="T">The type of wrapped value.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="value">The wrapped value to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder AddToSetWrapped<T>(string name, T value)
{
return new UpdateBuilder().AddToSetWrapped<T>(name, value);
}
/// <summary>
/// Sets the named element to the bitwise and of its value with another value (see $bit with "and").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be and-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder BitwiseAnd(string name, int value)
{
return new UpdateBuilder().BitwiseAnd(name, value);
}
/// <summary>
/// Sets the named element to the bitwise and of its value with another value (see $bit with "and").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be and-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder BitwiseAnd(string name, long value)
{
return new UpdateBuilder().BitwiseAnd(name, value);
}
/// <summary>
/// Sets the named element to the bitwise or of its value with another value (see $bit with "or").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be or-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder BitwiseOr(string name, int value)
{
return new UpdateBuilder().BitwiseOr(name, value);
}
/// <summary>
/// Sets the named element to the bitwise or of its value with another value (see $bit with "or").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be or-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder BitwiseOr(string name, long value)
{
return new UpdateBuilder().BitwiseOr(name, value);
}
/// <summary>
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be xor-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder BitwiseXor(string name, int value)
{
return new UpdateBuilder().BitwiseXor(name, value);
}
/// <summary>
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be xor-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder BitwiseXor(string name, long value)
{
return new UpdateBuilder().BitwiseXor(name, value);
}
/// <summary>
/// Combines several UpdateBuilders into a single UpdateBuilder.
/// </summary>
/// <param name="updates">The UpdateBuilders to combine.</param>
/// <returns>A combined UpdateBuilder.</returns>
public static UpdateBuilder Combine(IEnumerable<IMongoUpdate> updates)
{
if (updates == null) { throw new ArgumentNullException("updates"); }
var combined = new UpdateBuilder();
foreach (var update in updates)
{
combined.Combine(update);
}
return combined;
}
/// <summary>
/// Combines several UpdateBuilders into a single UpdateBuilder.
/// </summary>
/// <param name="updates">The UpdateBuilders to combine.</param>
/// <returns>A combined UpdateBuilder.</returns>
public static UpdateBuilder Combine(params IMongoUpdate[] updates)
{
return Combine((IEnumerable<IMongoUpdate>)updates);
}
/// <summary>
/// Sets the value of the named element to the current date (see $currentDate).
/// </summary>
/// <param name="name">The name of the element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder CurrentDate(string name)
{
return new UpdateBuilder().CurrentDate(name);
}
/// <summary>
/// Sets the value of the named element to the current date (see $currentDate).
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="type">The type to use.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder CurrentDate(string name, UpdateCurrentDateType type)
{
return new UpdateBuilder().CurrentDate(name, type);
}
/// <summary>
/// Increments the named element by a value (see $inc).
/// </summary>
/// <param name="name">The name of the element to be incremented.</param>
/// <param name="value">The value to increment by.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Inc(string name, double value)
{
return new UpdateBuilder().Inc(name, value);
}
/// <summary>
/// Increments the named element by a value (see $inc).
/// </summary>
/// <param name="name">The name of the element to be incremented.</param>
/// <param name="value">The value to increment by.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Inc(string name, int value)
{
return new UpdateBuilder().Inc(name, value);
}
/// <summary>
/// Increments the named element by a value (see $inc).
/// </summary>
/// <param name="name">The name of the element to be incremented.</param>
/// <param name="value">The value to increment by.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Inc(string name, long value)
{
return new UpdateBuilder().Inc(name, value);
}
/// <summary>
/// Updates the named element if and only if the provided value is greater than its current value (see $max).
/// </summary>
/// <param name="name">The name of the element to be updated.</param>
/// <param name="value">The value to use.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Max(string name, BsonValue value)
{
return new UpdateBuilder().Max(name, value);
}
/// <summary>
/// Updates the named element if and only if the provided value is less than its current value (see $max).
/// </summary>
/// <param name="name">The name of the element to be updated.</param>
/// <param name="value">The value to use.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Min(string name, BsonValue value)
{
return new UpdateBuilder().Min(name, value);
}
/// <summary>
/// Multiplies the named element by a value (see $mul).
/// </summary>
/// <param name="name">The name of the element to be multiplied.</param>
/// <param name="value">The value to multiply by.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Mul(string name, double value)
{
return new UpdateBuilder().Mul(name, value);
}
/// <summary>
/// Multiplies the named element by a value (see $mul).
/// </summary>
/// <param name="name">The name of the element to be multiplied.</param>
/// <param name="value">The value to multiply by.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Mul(string name, int value)
{
return new UpdateBuilder().Mul(name, value);
}
/// <summary>
/// Multiplies the named element by a value (see $mul).
/// </summary>
/// <param name="name">The name of the element to be multiplied.</param>
/// <param name="value">The value to multiply by.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Mul(string name, long value)
{
return new UpdateBuilder().Mul(name, value);
}
/// <summary>
/// Removes the first value from the named array element (see $pop).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PopFirst(string name)
{
return new UpdateBuilder().PopFirst(name);
}
/// <summary>
/// Removes the last value from the named array element (see $pop).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PopLast(string name)
{
return new UpdateBuilder().PopLast(name);
}
/// <summary>
/// Removes all values from the named array element that are equal to some value (see $pull).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="value">The value to remove.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Pull(string name, BsonValue value)
{
return new UpdateBuilder().Pull(name, value);
}
/// <summary>
/// Removes all values from the named array element that match some query (see $pull).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="query">A query that specifies which elements to remove.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Pull(string name, IMongoQuery query)
{
return new UpdateBuilder().Pull(name, query);
}
/// <summary>
/// Removes all values from the named array element that are equal to any of a list of values (see $pullAll).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to remove.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PullAll(string name, BsonArray values)
{
return new UpdateBuilder().PullAll(name, values);
}
/// <summary>
/// Removes all values from the named array element that are equal to any of a list of values (see $pullAll).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to remove.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PullAll(string name, IEnumerable<BsonValue> values)
{
return new UpdateBuilder().PullAll(name, values);
}
/// <summary>
/// Removes all values from the named array element that are equal to any of a list of values (see $pullAll).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to remove.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PullAll(string name, params BsonValue[] values)
{
return new UpdateBuilder().PullAll(name, values);
}
/// <summary>
/// Removes all values from the named array element that are equal to any of a list of wrapped values (see $pullAll).
/// </summary>
/// <typeparam name="T">The type of wrapped values.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The wrapped values to remove.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PullAllWrapped<T>(string name, IEnumerable<T> values)
{
return new UpdateBuilder().PullAllWrapped<T>(name, values);
}
/// <summary>
/// Removes all values from the named array element that are equal to any of a list of wrapped values (see $pullAll).
/// </summary>
/// <typeparam name="T">The type of wrapped values.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The wrapped values to remove.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PullAllWrapped<T>(string name, params T[] values)
{
return new UpdateBuilder().PullAllWrapped<T>(name, values);
}
/// <summary>
/// Removes all values from the named array element that are equal to some wrapped value (see $pull).
/// </summary>
/// <typeparam name="T">The type of wrapped value.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="value">The wrapped value to remove.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PullWrapped<T>(string name, T value)
{
return new UpdateBuilder().PullWrapped<T>(name, value);
}
/// <summary>
/// Adds a value to the end of the named array element (see $push).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="value">The value to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Push(string name, BsonValue value)
{
return new UpdateBuilder().Push(name, value);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $pushAll).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushAll(string name, BsonArray values)
{
return new UpdateBuilder().PushAll(name, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $pushAll).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushAll(string name, IEnumerable<BsonValue> values)
{
return new UpdateBuilder().PushAll(name, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $pushAll).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushAll(string name, params BsonValue[] values)
{
return new UpdateBuilder().PushAll(name, values);
}
/// <summary>
/// Adds a list of wrapped values to the end of the named array element (see $pushAll).
/// </summary>
/// <typeparam name="T">The type of wrapped values.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The wrapped values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushAllWrapped<T>(string name, IEnumerable<T> values)
{
return new UpdateBuilder().PushAllWrapped<T>(name, values);
}
/// <summary>
/// Adds a list of wrapped values to the end of the named array element (see $pushAll).
/// </summary>
/// <typeparam name="T">The type of wrapped values.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The wrapped values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushAllWrapped<T>(string name, params T[] values)
{
return new UpdateBuilder().PushAllWrapped<T>(name, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $push and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushEach(string name, BsonArray values)
{
return new UpdateBuilder().PushEach(name, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $push and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushEach(string name, IEnumerable<BsonValue> values)
{
return new UpdateBuilder().PushEach(name, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $push and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushEach(string name, params BsonValue[] values)
{
return new UpdateBuilder().PushEach(name, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $push and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="options">The options.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushEach(string name, PushEachOptions options, BsonArray values)
{
return new UpdateBuilder().PushEach(name, options, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $push and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="options">The options.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushEach(string name, PushEachOptions options, IEnumerable<BsonValue> values)
{
return new UpdateBuilder().PushEach(name, options, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $push and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="options">The options.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushEach(string name, PushEachOptions options, params BsonValue[] values)
{
return new UpdateBuilder().PushEach(name, options, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $push and $each).
/// </summary>
/// <typeparam name="T">The type of wrapped value.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushEachWrapped<T>(string name, IEnumerable<T> values)
{
return new UpdateBuilder().PushEachWrapped<T>(name, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $push and $each).
/// </summary>
/// <typeparam name="T">The type of wrapped value.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushEachWrapped<T>(string name, params T[] values)
{
return new UpdateBuilder().PushEachWrapped<T>(name, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $push and $each).
/// </summary>
/// <typeparam name="T">The type of wrapped value.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="options">The options.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushEachWrapped<T>(string name, PushEachOptions options, IEnumerable<T> values)
{
return new UpdateBuilder().PushEachWrapped<T>(name, options, values);
}
/// <summary>
/// Adds a list of values to the end of the named array element (see $push and $each).
/// </summary>
/// <typeparam name="T">The type of wrapped value.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="options">The options.</param>
/// <param name="values">The values to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushEachWrapped<T>(string name, PushEachOptions options, params T[] values)
{
return new UpdateBuilder().PushEachWrapped<T>(name, options, values);
}
/// <summary>
/// Adds a wrapped value to the end of the named array element (see $push).
/// </summary>
/// <typeparam name="T">The type of wrapped value.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="value">The wrapped value to add to the end of the array.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder PushWrapped<T>(string name, T value)
{
return new UpdateBuilder().PushWrapped<T>(name, value);
}
/// <summary>
/// Renames an element (see $rename).
/// </summary>
/// <param name="oldElementName">The name of the element to be renamed.</param>
/// <param name="newElementName">The new name of the element.</param>
/// <returns>An UpdateDocuemnt.</returns>
public static UpdateBuilder Rename(string oldElementName, string newElementName)
{
return new UpdateBuilder().Rename(oldElementName, newElementName);
}
/// <summary>
/// Replaces the entire document with a new document (the _id must remain the same).
/// </summary>
/// <typeparam name="TNominalType">The nominal type of the replacement document</typeparam>
/// <param name="document">The replacement document.</param>
/// <returns>An UpdateWrapper.</returns>
public static IMongoUpdate Replace<TNominalType>(TNominalType document)
{
if (document == null) { throw new ArgumentNullException("document"); }
var serializer = BsonSerializer.LookupSerializer<TNominalType>();
return new MongoUpdateWrapper(document, serializer, typeof(TNominalType));
}
/// <summary>
/// Replaces the entire document with a new document (the _id must remain the same).
/// </summary>
/// <param name="nominalType">The nominal type of the replacement document</param>
/// <param name="document">The replacement document.</param>
/// <returns>An UpdateWrapper.</returns>
public static IMongoUpdate Replace(Type nominalType, object document)
{
if (nominalType == null) { throw new ArgumentNullException("nominalType"); }
if (document == null) { throw new ArgumentNullException("document"); }
var serializer = BsonSerializer.LookupSerializer(nominalType);
return new MongoUpdateWrapper(document, serializer, nominalType);
}
/// <summary>
/// Sets the value of the named element to a new value (see $set).
/// </summary>
/// <param name="name">The name of the element to be set.</param>
/// <param name="value">The new value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Set(string name, BsonValue value)
{
return new UpdateBuilder().Set(name, value);
}
/// <summary>
/// Sets the value of the named element to the specified value only when an insert occurs
/// as part of an upsert operation (see $setOnInsert).
/// </summary>
/// <param name="name">The name of the element to be set.</param>
/// <param name="value">The new value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder SetOnInsert(string name, BsonValue value)
{
return new UpdateBuilder().SetOnInsert(name, value);
}
/// <summary>
/// Sets the value of the named element to a new wrapped value (see $set).
/// </summary>
/// <typeparam name="T">The type of wrapped value.</typeparam>
/// <param name="name">The name of the element to be set.</param>
/// <param name="value">The new wrapped value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder SetWrapped<T>(string name, T value)
{
return new UpdateBuilder().SetWrapped<T>(name, value);
}
/// <summary>
/// Removes the named element from the document (see $unset).
/// </summary>
/// <param name="name">The name of the element to be removed.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder Unset(string name)
{
return new UpdateBuilder().Unset(name);
}
}
/// <summary>
/// A builder for creating update modifiers.
/// </summary>
[Serializable]
[BsonSerializer(typeof(UpdateBuilder.Serializer))]
public class UpdateBuilder : BuilderBase, IMongoUpdate
{
// private fields
private BsonDocument _document;
// constructors
/// <summary>
/// Initializes a new instance of the UpdateBuilder class.
/// </summary>
public UpdateBuilder()
{
_document = new BsonDocument();
}
// internal properties
internal BsonDocument Document
{
get { return _document; }
}
// public methods
/// <summary>
/// Adds a value to a named array element if the value is not already in the array (see $addToSet).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="value">The value to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder AddToSet(string name, BsonValue value)
{
if (name == null) { throw new ArgumentNullException("name"); }
if (value == null) { throw new ArgumentNullException("value"); }
BsonElement element;
if (_document.TryGetElement("$addToSet", out element))
{
element.Value.AsBsonDocument.Add(name, value);
}
else
{
_document.Add("$addToSet", new BsonDocument(name, value));
}
return this;
}
/// <summary>
/// Adds a list of values to a named array element adding each value only if it not already in the array (see $addToSet and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder AddToSetEach(string name, BsonArray values)
{
if (name == null) { throw new ArgumentNullException("name"); }
if (values == null) { throw new ArgumentNullException("values"); }
var arg = new BsonDocument("$each", values);
BsonElement element;
if (_document.TryGetElement("$addToSet", out element))
{
element.Value.AsBsonDocument.Add(name, arg);
}
else
{
_document.Add("$addToSet", new BsonDocument(name, arg));
}
return this;
}
/// <summary>
/// Adds a list of values to a named array element adding each value only if it not already in the array (see $addToSet and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder AddToSetEach(string name, IEnumerable<BsonValue> values)
{
if (name == null) { throw new ArgumentNullException("name"); }
if (values == null) { throw new ArgumentNullException("values"); }
return AddToSetEach(name, new BsonArray(values));
}
/// <summary>
/// Adds a list of values to a named array element adding each value only if it not already in the array (see $addToSet and $each).
/// </summary>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The values to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder AddToSetEach(string name, params BsonValue[] values)
{
if (name == null) { throw new ArgumentNullException("name"); }
if (values == null) { throw new ArgumentNullException("values"); }
return AddToSetEach(name, (IEnumerable<BsonValue>)values);
}
/// <summary>
/// Adds a list of wrapped values to a named array element adding each value only if it not already in the array (see $addToSet and $each).
/// </summary>
/// <typeparam name="T">The type of wrapped values.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The wrapped values to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder AddToSetEachWrapped<T>(string name, IEnumerable<T> values)
{
if (name == null) { throw new ArgumentNullException("name"); }
if (values == null) { throw new ArgumentNullException("values"); }
var wrappedValues = BsonDocumentWrapper.CreateMultiple(values).Cast<BsonValue>(); // the cast to BsonValue is required
return AddToSetEach(name, wrappedValues);
}
/// <summary>
/// Adds a list of wrapped values to a named array element adding each value only if it not already in the array (see $addToSet and $each).
/// </summary>
/// <typeparam name="T">The type of wrapped values.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="values">The wrapped values to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder AddToSetEachWrapped<T>(string name, params T[] values)
{
if (name == null) { throw new ArgumentNullException("name"); }
if (values == null) { throw new ArgumentNullException("values"); }
return AddToSetEachWrapped(name, (IEnumerable<T>)values);
}
/// <summary>
/// Adds a wrapped value to a named array element if the value is not already in the array (see $addToSet).
/// </summary>
/// <typeparam name="T">The type of wrapped value.</typeparam>
/// <param name="name">The name of the array element.</param>
/// <param name="value">The wrapped value to add to the set.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder AddToSetWrapped<T>(string name, T value)
{
if (name == null) { throw new ArgumentNullException("name"); }
var wrappedValue = (BsonValue)BsonDocumentWrapper.Create(value); // the cast to BsonValue is required
return AddToSet(name, wrappedValue);
}
/// <summary>
/// Sets the named element to the bitwise and of its value with another value (see $bit with "and").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be and-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder BitwiseAnd(string name, int value)
{
if (name == null) { throw new ArgumentNullException("name"); }
BitwiseOperation(name, "and", value);
return this;
}
/// <summary>
/// Sets the named element to the bitwise and of its value with another value (see $bit with "and").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be and-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder BitwiseAnd(string name, long value)
{
if (name == null) { throw new ArgumentNullException("name"); }
BitwiseOperation(name, "and", value);
return this;
}
/// <summary>
/// Sets the named element to the bitwise or of its value with another value (see $bit with "or").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be or-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder BitwiseOr(string name, int value)
{
if (name == null) { throw new ArgumentNullException("name"); }
BitwiseOperation(name, "or", value);
return this;
}
/// <summary>
/// Sets the named element to the bitwise or of its value with another value (see $bit with "or").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be or-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder BitwiseOr(string name, long value)
{
if (name == null) { throw new ArgumentNullException("name"); }
BitwiseOperation(name, "or", value);
return this;
}
/// <summary>
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be xor-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder BitwiseXor(string name, int value)
{
if (name == null) { throw new ArgumentNullException("name"); }
BitwiseOperation(name, "xor", value);
return this;
}
/// <summary>
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be xor-ed with the current value.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder BitwiseXor(string name, long value)
{
if (name == null) { throw new ArgumentNullException("name"); }
BitwiseOperation(name, "xor", value);
return this;
}
/// <summary>
/// Combines another UpdateBuilder into this one.
/// </summary>
/// <param name="other">The IMongoUpdate to combine into this one.</param>
/// <returns>A combined UpdateBuilder.</returns>
public UpdateBuilder Combine(IMongoUpdate other)
{
if (other == null) { throw new ArgumentNullException("other"); }
var otherUpdate = other.ToBsonDocument();
foreach (var otherOperation in otherUpdate)
{
var otherOperationName = otherOperation.Name;
var otherTargets = otherOperation.Value.AsBsonDocument;
BsonElement operation;
if (_document.TryGetElement(otherOperationName, out operation))
{
operation.Value.AsBsonDocument.AddRange(otherTargets);
}
else
{
_document.Add(otherOperationName, otherTargets);
}
}
return this;
}
/// <summary>
/// Sets the value of the named element to the current date (see $currentDate).
/// </summary>
/// <param name="name">The name of the element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder CurrentDate(string name)
{
if (name == null) { throw new ArgumentNullException("name"); }
BsonElement element;
if (_document.TryGetElement("$currentDate", out element))
{
element.Value.AsBsonDocument.Add(name, true);
}
else
{
_document.Add("$currentDate", new BsonDocument(name, true));
}
return this;
}
/// <summary>
/// Sets the value of the named element to the current date (see $currentDate).
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="type">The type to use.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder CurrentDate(string name, UpdateCurrentDateType type)
{
if (name == null) { throw new ArgumentNullException("name"); }
var op = type == UpdateCurrentDateType.Date ?
new BsonDocument("$type", "date") :
new BsonDocument("$type", "timestamp");
BsonElement element;
if (_document.TryGetElement("$currentDate", out element))
{
element.Value.AsBsonDocument.Add(name, op);
}
else
{
_document.Add("$currentDate", new BsonDocument(name, op));
}
return this;
}