docs: @SuppressWarnings("deprecation")
      public Object toMongoObject(final MappedField mf, final MappedClass mc, final Object value) {
          if (value == null) {
              return null;
          }
  
          Object mappedValue = value;
  
          if (value instanceof Query) {
              mappedValue = ((QueryImpl) value).getQueryObject();
          } else if (isAssignable(mf, value) || isEntity(mc)) {
              //convert the value to Key (DBRef) if the field is @Reference or type is Key/DBRef, or if the destination class is an @Entity
              try {
                  if (value instanceof Iterable) {
                      MappedClass mapped = getMappedClass(mf.getSubClass());
                      if (mapped != null && (Key.class.isAssignableFrom(mapped.getClazz()) || mapped.getEntityAnnotation() != null)) {
                          mappedValue = getDBRefs(mf, (Iterable) value);
                      } else {
                          if (mf.hasAnnotation(Reference.class)) {
                              mappedValue = getDBRefs(mf, (Iterable) value);
                          } else {
                              mappedValue = toMongoObject(value, false);
                          }
                      }
                  } else {
                      if (mf != null) {
                          Class<?> idType = null;
                          if (!mf.getType().equals(Key.class) && isMapped(mf.getType())) {
                              final MappedField idField = getMappedClass(mf.getType())
                                                              .getMappedIdField();
                              idType = idField != null ? idField.getType() : null;
                          }
                          boolean valueIsIdType = mappedValue.getClass().equals(idType);
                          Reference refAnn = mf.getAnnotation(Reference.class);
                          if (refAnn != null) {
                              if (!valueIsIdType) {
                                  Key<?> key = value instanceof Key ? (Key<?>) value : getKey(value);
                                  if (key != null) {
                                      mappedValue = refAnn.idOnly()
                                                    ? keyToId(key)
                                                    : keyToDBRef(key);
                                  }
                              }
                          } else if (mf.getType().isAssignableFrom(MorphiaReference.class)) {
                              if (!valueIsIdType) {
                                  Key<?> key = value instanceof Key ? (Key<?>) value : getKey(value);
                                  if (key != null) {
                                      mappedValue = keyToId(key);
                                  }
                              }
  
                          } else if (mf.getType().equals(Key.class)) {
                              mappedValue = keyToDBRef(valueIsIdType
                                                       ? createKey(mf.getSubClass(), value)
                                                       : value instanceof Key ? (Key<?>) value : getKey(value));
                              if (mappedValue == value) {
                                  throw new ValidationException("cannot map to Key<T> field: " + value);
                              }
                          }
                      }
  
                      if (mappedValue == value) {
                          mappedValue = toMongoObject(value, false);
                      }
                  }
              } catch (Exception e) {
                  LOG.error("Error converting value(" + value + ") to reference.", e);
                  mappedValue = toMongoObject(value, false);
              }
          } else if (mf != null && mf.hasAnnotation(Serialized.class)) { //serialized
              try {
                  mappedValue = Serializer.serialize(value, !mf.getAnnotation(Serialized.class).disableCompression());
              } catch (IOException e) {
                  throw new RuntimeException(e);
              }
          } else if (value instanceof DBObject) {  //pass-through
              mappedValue = value;
          } else {
              mappedValue = toMongoObject(value, EmbeddedMapper.shouldSaveClassName(value, mappedValue, mf));
              if (mappedValue instanceof BasicDBList) {
                  final BasicDBList list = (BasicDBList) mappedValue;
                  if (list.size() != 0) {
                      if (!EmbeddedMapper.shouldSaveClassName(extractFirstElement(value), list.get(0), mf)) {
                          for (Object o : list) {
                              if (o instanceof DBObject) {
                                  ((DBObject) o).removeField(opts.getDiscriminatorField());
                              }
                          }
                      }
                  }
              } else if (mappedValue instanceof DBObject && !EmbeddedMapper.shouldSaveClassName(value, mappedValue, mf)) {
                  ((DBObject) mappedValue).removeField(opts.getDiscriminatorField());
              }
          }
  
          return mappedValue;
      }