@@ -635,36 +635,58 @@ public virtual FindAndModifyResult FindAndModify(FindAndModifyArgs args)
635635 if ( args == null ) { throw new ArgumentNullException ( "args" ) ; }
636636 if ( args . Update == null ) { throw new ArgumentException ( "Update is null." , "args" ) ; }
637637
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 ) )
650639 {
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
656661 {
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
659680 {
660681 { "value" , BsonNull . Value } ,
661682 { "ok" , true }
662683 } ;
663684#pragma warning disable 618
664- return new FindAndModifyResult ( response ) { Command = command } ;
685+ return new FindAndModifyResult ( response ) { Command = command } ;
665686#pragma warning restore
687+ }
688+ throw ;
666689 }
667- throw ;
668690 }
669691 }
670692
@@ -689,35 +711,57 @@ public virtual FindAndModifyResult FindAndRemove(IMongoQuery query, IMongoSortBy
689711 public virtual FindAndModifyResult FindAndRemove ( FindAndRemoveArgs args )
690712 {
691713 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 ) )
707716 {
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 )
709721 {
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
712755 {
713756 { "value" , BsonNull . Value } ,
714757 { "ok" , true }
715758 } ;
716759#pragma warning disable 618
717- return new FindAndModifyResult ( response ) { Command = command } ;
760+ return new FindAndModifyResult ( response ) { Command = command } ;
718761#pragma warning restore
762+ }
763+ throw ;
719764 }
720- throw ;
721765 }
722766 }
723767
0 commit comments