public static BsonValue encode(Object value) {
        return switch (value) {
            case BsonValue val -> val;
            case Collection<?> values -> {
                int size = values.size();
                List<BsonValue> list = new ArrayList<>(size);
                for (Object val : values) {
                    list.add(encodeSingleValue(val));
                }
                yield BsonArrayUtil.newArray(list);
            }
            case Map<?, ?> map -> {
                int size = map.size();
                Object key;
                BsonDocument document = new BsonDocument(CollectionUtil.getMapCapability(size));
                for (Map.Entry<?, ?> entry : map.entrySet()) {
                    key = entry.getKey();
                    switch (key) {
                        case String str -> document.put(str, encode(entry.getValue()));
                        case Number number ->
                            document.put(number.toString(), encode(entry.getValue()));
                        default -> throw new IllegalArgumentException(
                                "Expecting the map key to be a string or number, but got: "
                                        + key);
                    }
                }
                yield document;
            }
            case byte[] bytes -> new BsonBinary(bytes);
            default -> {
                Class<?> clazz = value.getClass();
                if (clazz.isArray()) {
                    int size = Array.getLength(value);
                    List<BsonValue> list = new ArrayList<>(size);
                    for (int i = 0; i < size; i++) {
                        list.add(encodeSingleValue(Array.get(value, i)));
                    }
                    yield BsonArrayUtil.newArray(list);
                }
                yield encodeSingleValue(value);
            }
        };
    }
TODO: unify the logic of {@link #encode} and {@link #write}
turms-server-common/src/main/java/im/turms/server/common/storage/mongo/codec/CodecUtil.java