private void initInstance(T instance, BsonReader reader, DecoderContext decoderContext) {
        reader.readStartDocument();
        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            String fieldName = reader.readName();
            EntityField field = DomainFieldName.ID.equals(fieldName)
                    ? entity.getField(entity.idFieldName())
                    : entity.getField(fieldName);
            if (field == null) {
                if (fieldName.startsWith(USER_DEFINED_ATTRIBUTE_PREFIX)) {
                    field = entity.getField(Customizable.USER_DEFINED_ATTRIBUTES_FIELD_NAME);
                    if (field != null) {
                        Map<String, Object> userDefinedAttributes =
                                (Map<String, Object>) field.get(instance);
                        if (userDefinedAttributes == null) {
                            userDefinedAttributes = new HashMap<>(16);
                            field.set(instance, userDefinedAttributes);
                        }
                        userDefinedAttributes.put(
                                fieldName.substring(USER_DEFINED_ATTRIBUTE_PREFIX_LENGTH),
                                CodecUtil.read(reader));
                        continue;
                    }
                }
                LOGGER.warn("The field \"{}\" does not exist in the entity class: {}",
                        fieldName,
                        entity.entityClass()
                                .getName());
                reader.skipValue();
            } else {
                Object value = null;
                if (reader.getCurrentBsonType() == BsonType.NULL) {
                    reader.readNull();
                } else {
                    try {
                        value = decode(field, reader, decoderContext);
                    } catch (Exception e) {
                        throw new DeserializationException(
                                "Failed to decode the field \""
                                        + fieldName
                                        + "\" of the entity class: "
                                        + entity.entityClass()
                                                .getName(),
                                e);
                    }
                }
                try {
                    field.set(instance, value);
                } catch (Exception e) {
                    throw new DeserializationException(
                            "Failed to set the field \""
                                    + fieldName
                                    + "\" of the entity class: "
                                    + entity.entityClass()
                                            .getName(),
                            e);
                }
            }
        }
        reader.readEndDocument();
    }
Compared to {@link EntityCodec#encodeValue}, this method encodes null fields because we need
to overwrite existing fields to null if these fields are not specified in the entity for our
use cases.
turms-server-common/src/main/java/im/turms/server/common/storage/mongo/codec/EntityCodec.java