code: public static int extractErrorCode(final BsonDocument response) {
          // mongos may set an err field containing duplicate key error information
          String errorMessage = extractErrorMessage(response);
          if (errorMessage != null) {
              if (response.containsKey("err")) {
                  if (errorMessage.contains("E11000 duplicate key error")) {
                      return 11000;
                  }
              }
  
              // mongos may return a list of documents representing write command responses from each shard.  Return the one with a matching
              // "err" field, so that it can be used to get the error code
              if (!response.containsKey("code") && response.containsKey("errObjects")) {
                  for (BsonValue curErrorDocument : response.getArray("errObjects")) {
                      if (errorMessage.equals(extractErrorMessage(curErrorDocument.asDocument()))) {
                          return curErrorDocument.asDocument().getNumber("code").intValue();
                      }
                  }
              }
          }
          return response.getNumber("code", new BsonInt32(-1)).intValue();
      }
docs: For internal use only: extract the error code from the response to a write command.
  @param response the response
  @return the code, or -1 if there is none