def resolve_nested_documents(updates, original):
 r = {}
 for (field, value) in updates.items():
  if isinstance(value, dict):
   orig_value = original.setdefault(field, {})
   if (orig_value is None):
    r[field] = value
   else:
    orig_value.update(resolve_nested_documents(value, orig_value))
    r[field] = orig_value
  else:
   r[field] = value
 return r

def resolve_nested_documents ( updates , original ) : r = { } for ( field , value ) in updates . items ( ) : if isinstance ( value , dict ) : orig_value = original . setdefault ( field , { } ) if ( orig_value is None ) : r [ field ] = value else : orig_value . update ( resolve_nested_documents ( value , orig_value ) ) r [ field ] = orig_value else : r [ field ] = value return r
nested document updates are merged with the original contents we dont overwrite the whole thing .