code: def final_save(self, attempts):
      import pymongo.errors
  
      for i in range(attempts):
          try:
              self.runs.update_one(
                  {"_id": self.run_entry["_id"]},
                  {"$set": self.run_entry},
                  upsert=True,
              )
              return
          except pymongo.errors.AutoReconnect:
              if i < attempts - 1:
                  time.sleep(1)
          except pymongo.errors.ConnectionFailure:
              pass
          except pymongo.errors.InvalidDocument:
              self.run_entry = force_bson_encodeable(self.run_entry)
              print(
                  "Warning: Some of the entries of the run were not "
                  "BSON-serializable!\n They have been altered such that "
                  "they can be stored, but you should fix your experiment!"
                  "Most likely it is either the 'info' or the 'result'.",
                  file=sys.stderr,
              )
  
      os.makedirs(self.failure_dir, exist_ok=True)
      with NamedTemporaryFile(
          suffix=".pickle",
          delete=False,
          prefix="sacred_mongo_fail_{}_".format(self.run_entry["_id"]),
          dir=self.failure_dir,
      ) as f:
          pickle.dump(self.run_entry, f)
          print(
              "Warning: saving to MongoDB failed! "
              "Stored experiment entry in '{}'".format(f.name),
              file=sys.stderr,
          )
  
imports: from typing import Optional, Union
  import mimetypes
  import os.path
  import pickle
  import re
  import sys
  import time
  from tempfile import NamedTemporaryFile
  import warnings
  import sacred.optional
  from sacred.commandline_options import cli_option
  from sacred.dependencies import get_digest
  from sacred.observers.base import RunObserver
  from sacred.observers.queue import QueueObserver
  from sacred.serializer import flatten
  from sacred.utils import ObserverError, PathType
  import pkg_resources
label_desc: Use image node_id 17 for calling the MongoObserver obj's underlying member method code with example usage: obj.final_save(attempts) without return types