docs: function updateDocument(existingDoc, newDoc, id, db, f_ShouldUpdate, shouldCreate, f_Merge, callback) {
      if (!f_ShouldUpdate) {
          f_ShouldUpdate = always;
      }
  
      if (!f_Merge) {
          f_Merge = useNewVersion;
      }
  
      // If we never grabbed the existing document from the DB, fetch it and try again.
      if (!existingDoc) {
          return db.get(id, function (err, r) {
              // If the fetch came back with nothing, attempt to do a fresh insert unless the user
              // doesn't want documents to be created.
              if (err && (err.error === "not_found")) {
                  if (!shouldCreate)
                      // Don't create a document where one didn't exist, if the user so desires.
                      return callback(null, null);
  
                  return db.insert(newDoc, id, function (err, r) {
                      // If someone beat us here, we should retry this whole operation.
                      if (err && err.error === "conflict")
                          return updateDocument(null, newDoc, id, db, f_ShouldUpdate, shouldCreate, f_Merge, callback);
  
                      // If anything else happened, we should bail.
                      if (err)
                          return callback("Could not fetch existing document: " + err.toString());
  
                      // The insert worked.  We're done.
                      return callback(null, extend({}, newDoc, {_rev: r.rev}));
                  });
              }
  
              // If the fetch came back with any other error, bail.
              if (err)
                  return callback("Could not fetch existing document: " + err.toString());
  
              // We got the document.  Let's try the update again.
              return updateDocument(r, newDoc, id, db, f_ShouldUpdate, shouldCreate, f_Merge, callback);
          });
      }
  
      if (!f_ShouldUpdate(existingDoc, newDoc)) {
          return callback(null, existingDoc);
      }
  
      // Before merging, record that we want to undelete a document that has been deleted.
      var mergedDoc = f_Merge(omit(existingDoc, "_deleted"), newDoc);
      if (!mergedDoc || typeof mergedDoc !== "object" || mergedDoc instanceof Error) {
          // Allow a user defined merge algorithm to say ``sorry...can't merge'' by returning a non-object or Error.
          // Bubble this error, so that the user can then test for it in their callback.
          return callback(mergedDoc);
      }
  
      // It's essential that we specify a revision or this will never terminate.
      // It's probably not a good idea to trust a user-specified merge function to do this.
      mergedDoc = extend({}, mergedDoc, { _rev: existingDoc._rev });
  
      // The document exists, and is out of date.  We need to overwrite it.
      db.insert(
          mergedDoc,
          id,
          function (err, result) {
              // If someone beat us here, we need to try the whole thing again.
              if (err && err.error === "conflict")
                  return updateDocument(null, newDoc, id, db, f_ShouldUpdate, shouldCreate, f_Merge, callback);
  
              // If anything else happened, bail.
              if (err)
                  return callback("Could not update the document to the proposed version: " + err.toString());
  
              // Update the new document's _rev
              mergedDoc._rev = result.rev;
  
              // The replacement worked.  We're done.  Finally!
              return callback(null, mergedDoc);
          }
      );
  }