@@ -635,36 +635,58 @@ public virtual FindAndModifyResult FindAndModify(FindAndModifyArgs args)
635
635
if ( args == null ) { throw new ArgumentNullException ( "args" ) ; }
636
636
if ( args . Update == null ) { throw new ArgumentException ( "Update is null." , "args" ) ; }
637
637
638
- var command = new CommandDocument
639
- {
640
- { "findAndModify" , _name } ,
641
- { "query" , ( ) => BsonDocumentWrapper . Create ( args . Query ) , args . Query != null } , // optional
642
- { "sort" , ( ) => BsonDocumentWrapper . Create ( args . SortBy ) , args . SortBy != null } , // optional
643
- { "update" , BsonDocumentWrapper . Create ( args . Update , true ) } , // isUpdateDocument = true
644
- { "new" , ( ) => args . VersionReturned . Value == FindAndModifyDocumentVersion . Modified , args . VersionReturned . HasValue } , // optional
645
- { "fields" , ( ) => BsonDocumentWrapper . Create ( args . Fields ) , args . Fields != null } , // optional
646
- { "upsert" , true , args . Upsert } , // optional
647
- { "maxTimeMS" , ( ) => args . MaxTime . Value . TotalMilliseconds , args . MaxTime . HasValue } // optional
648
- } ;
649
- try
638
+ using ( var request = _server . RequestStart ( null ) )
650
639
{
651
- return RunCommandAs < FindAndModifyResult > ( command ) ;
652
- }
653
- catch ( MongoCommandException ex )
654
- {
655
- if ( ex . ErrorMessage == "No matching object found" )
640
+ var serverInstance = _server . RequestConnection . ServerInstance ;
641
+
642
+ var writeConcern = _settings . WriteConcern . ToBsonDocument ( ) ;
643
+ if ( writeConcern . ElementCount == 0 )
644
+ {
645
+ writeConcern = null ;
646
+ }
647
+
648
+ var command = new CommandDocument
649
+ {
650
+ { "findAndModify" , _name } ,
651
+ { "query" , ( ) => BsonDocumentWrapper . Create ( args . Query ) , args . Query != null } , // optional
652
+ { "sort" , ( ) => BsonDocumentWrapper . Create ( args . SortBy ) , args . SortBy != null } , // optional
653
+ { "update" , BsonDocumentWrapper . Create ( args . Update , true ) } , // isUpdateDocument = true
654
+ { "new" , ( ) => args . VersionReturned . Value == FindAndModifyDocumentVersion . Modified , args . VersionReturned . HasValue } , // optional
655
+ { "fields" , ( ) => BsonDocumentWrapper . Create ( args . Fields ) , args . Fields != null } , // optional
656
+ { "upsert" , true , args . Upsert } , // optional
657
+ { "maxTimeMS" , ( ) => args . MaxTime . Value . TotalMilliseconds , args . MaxTime . HasValue } , // optional
658
+ { "writeConcern" , writeConcern , writeConcern != null && serverInstance . Supports ( FeatureId . FindAndModifyWriteConcern ) }
659
+ } ;
660
+ try
656
661
{
657
- // create a new command result with what the server should have responded
658
- var response = new BsonDocument
662
+ var result = RunCommandAs < FindAndModifyResult > ( command ) ;
663
+
664
+ BsonValue writeConcernError ;
665
+ if ( result . Response . TryGetValue ( "writeConcernError" , out writeConcernError ) )
666
+ {
667
+ var message = writeConcernError [ "errmsg" ] . AsString ;
668
+ var writeConcernResult = new WriteConcernResult ( result . Response ) ;
669
+ throw new MongoWriteConcernException ( message , writeConcernResult ) ;
670
+ }
671
+
672
+ return result ;
673
+ }
674
+ catch ( MongoCommandException ex )
675
+ {
676
+ if ( ex . ErrorMessage == "No matching object found" )
677
+ {
678
+ // create a new command result with what the server should have responded
679
+ var response = new BsonDocument
659
680
{
660
681
{ "value" , BsonNull . Value } ,
661
682
{ "ok" , true }
662
683
} ;
663
684
#pragma warning disable 618
664
- return new FindAndModifyResult ( response ) { Command = command } ;
685
+ return new FindAndModifyResult ( response ) { Command = command } ;
665
686
#pragma warning restore
687
+ }
688
+ throw ;
666
689
}
667
- throw ;
668
690
}
669
691
}
670
692
@@ -689,35 +711,57 @@ public virtual FindAndModifyResult FindAndRemove(IMongoQuery query, IMongoSortBy
689
711
public virtual FindAndModifyResult FindAndRemove ( FindAndRemoveArgs args )
690
712
{
691
713
if ( args == null ) { throw new ArgumentNullException ( "args" ) ; }
692
-
693
- var command = new CommandDocument
694
- {
695
- { "findAndModify" , _name } ,
696
- { "query" , ( ) => BsonDocumentWrapper . Create ( args . Query ) , args . Query != null } , // optional
697
- { "sort" , ( ) => BsonDocumentWrapper . Create ( args . SortBy ) , args . SortBy != null } , // optional
698
- { "remove" , true } ,
699
- { "fields" , ( ) => BsonDocumentWrapper . Create ( args . Fields ) , args . Fields != null } , // optional
700
- { "maxTimeMS" , ( ) => args . MaxTime . Value . TotalMilliseconds , args . MaxTime . HasValue } // optional
701
- } ;
702
- try
703
- {
704
- return RunCommandAs < FindAndModifyResult > ( command ) ;
705
- }
706
- catch ( MongoCommandException ex )
714
+
715
+ using ( var request = _server . RequestStart ( null ) )
707
716
{
708
- if ( ex . ErrorMessage == "No matching object found" )
717
+ var serverInstance = _server . RequestConnection . ServerInstance ;
718
+
719
+ var writeConcern = _settings . WriteConcern . ToBsonDocument ( ) ;
720
+ if ( writeConcern . ElementCount == 0 )
709
721
{
710
- // create a new command result with what the server should have responded
711
- var response = new BsonDocument
722
+ writeConcern = null ;
723
+ }
724
+
725
+ var command = new CommandDocument
726
+ {
727
+ { "findAndModify" , _name } ,
728
+ { "query" , ( ) => BsonDocumentWrapper . Create ( args . Query ) , args . Query != null } , // optional
729
+ { "sort" , ( ) => BsonDocumentWrapper . Create ( args . SortBy ) , args . SortBy != null } , // optional
730
+ { "remove" , true } ,
731
+ { "fields" , ( ) => BsonDocumentWrapper . Create ( args . Fields ) , args . Fields != null } , // optional
732
+ { "maxTimeMS" , ( ) => args . MaxTime . Value . TotalMilliseconds , args . MaxTime . HasValue } , // optional
733
+ { "writeConcern" , writeConcern , writeConcern != null && serverInstance . Supports ( FeatureId . FindAndModifyWriteConcern ) }
734
+ } ;
735
+ try
736
+ {
737
+ var result = RunCommandAs < FindAndModifyResult > ( command ) ;
738
+
739
+ BsonValue writeConcernError ;
740
+ if ( result . Response . TryGetValue ( "writeConcernError" , out writeConcernError ) )
741
+ {
742
+ var message = writeConcernError [ "errmsg" ] . AsString ;
743
+ var writeConcernResult = new WriteConcernResult ( result . Response ) ;
744
+ throw new MongoWriteConcernException ( message , writeConcernResult ) ;
745
+ }
746
+
747
+ return result ;
748
+ }
749
+ catch ( MongoCommandException ex )
750
+ {
751
+ if ( ex . ErrorMessage == "No matching object found" )
752
+ {
753
+ // create a new command result with what the server should have responded
754
+ var response = new BsonDocument
712
755
{
713
756
{ "value" , BsonNull . Value } ,
714
757
{ "ok" , true }
715
758
} ;
716
759
#pragma warning disable 618
717
- return new FindAndModifyResult ( response ) { Command = command } ;
760
+ return new FindAndModifyResult ( response ) { Command = command } ;
718
761
#pragma warning restore
762
+ }
763
+ throw ;
719
764
}
720
- throw ;
721
765
}
722
766
}
723
767
0 commit comments