@SuppressWarnings("unchecked")
    @Override
    public Object visit(Operation<?> expr, Void context) {
        Operator op = expr.getOperator();
        if (op == Ops.EQ) {
            if (expr.getArg(0) instanceof Operation) {
                Operation<?> lhs = (Operation<?>) expr.getArg(0);
                if (lhs.getOperator() == Ops.COL_SIZE || lhs.getOperator() == Ops.ARRAY_SIZE) {
                    return asDocument(asDBKey(lhs, 0), asDocument("$size", asDBValue(expr, 1)));
                } else {
                    throw new UnsupportedOperationException("Illegal operation " + expr);
                }
            } else if (expr.getArg(0) instanceof Path) {
                Path<?> path = (Path<?>) expr.getArg(0);
                Constant<?> constant = (Constant<?>) expr.getArg(1);
                return asDocument(asDBKey(expr, 0), convert(path, constant));
            }
        } else if (op == Ops.STRING_IS_EMPTY) {
            return asDocument(asDBKey(expr, 0), "");

        } else if (op == Ops.AND) {
            Queue<Map<Object, Object>> pendingDocuments = collectConnectorArgs("$and", expr);
            List<Map<Object, Object>> unmergeableDocuments = new ArrayList<Map<Object, Object>>();
            List<Map<Object, Object>> generatedDocuments = new ArrayList<Map<Object, Object>>();

            while (!pendingDocuments.isEmpty()) {

                Map<Object, Object> lhs = pendingDocuments.poll();

                for (Map<Object, Object> rhs : pendingDocuments) {
                    Set<Object> lhs2 = new LinkedHashSet<Object>(lhs.keySet());
                    lhs2.retainAll(rhs.keySet());
                    if (lhs2.isEmpty()) {
                        lhs.putAll(rhs);
                    } else {
                        unmergeableDocuments.add(rhs);
                    }
                }

                generatedDocuments.add(lhs);
                pendingDocuments = new LinkedList<Map<Object, Object>>(unmergeableDocuments);
                unmergeableDocuments = new LinkedList<Map<Object, Object>>();
            }

            return generatedDocuments.size() == 1 ? generatedDocuments.get(0) : asDocument("$and", generatedDocuments);
        } else if (op == Ops.NOT) {
            //Handle the not's child
            Operation<?> subOperation = (Operation<?>) expr.getArg(0);
            Operator subOp = subOperation.getOperator();
            if (subOp == Ops.IN) {
                return visit(ExpressionUtils.operation(Boolean.class, Ops.NOT_IN, subOperation.getArg(0),
                        subOperation.getArg(1)), context);
            } else {
                Document arg = (Document) handle(expr.getArg(0));
                return negate(arg);
            }

        } else if (op == Ops.OR) {
            return asDocument("$or", collectConnectorArgs("$or", expr));

        } else if (op == Ops.NE) {
            Path<?> path = (Path<?>) expr.getArg(0);
            Constant<?> constant = (Constant<?>) expr.getArg(1);
            return asDocument(asDBKey(expr, 0), asDocument("$ne", convert(path, constant)));

        } else if (op == Ops.STARTS_WITH) {
            return asDocument(asDBKey(expr, 0), new BsonRegularExpression("^" + regexValue(expr, 1)));

        } else if (op == Ops.STARTS_WITH_IC) {
            return asDocument(asDBKey(expr, 0), new BsonRegularExpression("^" + regexValue(expr, 1), "i"));

        } else if (op == Ops.ENDS_WITH) {
            return asDocument(asDBKey(expr, 0), new BsonRegularExpression(regexValue(expr, 1) + "$"));

        } else if (op == Ops.ENDS_WITH_IC) {
            return asDocument(asDBKey(expr, 0), new BsonRegularExpression(regexValue(expr, 1) + "$", "i"));

        } else if (op == Ops.EQ_IGNORE_CASE) {
            return asDocument(asDBKey(expr, 0), new BsonRegularExpression("^" + regexValue(expr, 1) + "$", "i"));

        } else if (op == Ops.STRING_CONTAINS) {
            return asDocument(asDBKey(expr, 0), new BsonRegularExpression(".*" + regexValue(expr, 1) + ".*"));

        } else if (op == Ops.STRING_CONTAINS_IC) {
            return asDocument(asDBKey(expr, 0), new BsonRegularExpression(".*" + regexValue(expr, 1) + ".*", "i"));

        } else if (op == Ops.MATCHES) {
            return asDocument(asDBKey(expr, 0), new BsonRegularExpression(asDBValue(expr, 1).toString()));

        } else if (op == Ops.MATCHES_IC) {
            return asDocument(asDBKey(expr, 0), new BsonRegularExpression(asDBValue(expr, 1).toString(), "i"));

        } else if (op == Ops.LIKE) {
            String regex = ExpressionUtils.likeToRegex((Expression) expr.getArg(1)).toString();
            return asDocument(asDBKey(expr, 0), new BsonRegularExpression(regex));

        } else if (op == Ops.LIKE_IC) {
            String regex = ExpressionUtils.likeToRegex((Expression) expr.getArg(1)).toString();
            return asDocument(asDBKey(expr, 0), new BsonRegularExpression(regex, "i"));

        } else if (op == Ops.BETWEEN) {
            Document value = new Document("$gte", asDBValue(expr, 1));
            value.append("$lte", asDBValue(expr, 2));
            return asDocument(asDBKey(expr, 0), value);

        } else if (op == Ops.IN) {
            int constIndex = 0;
            int exprIndex = 1;
            if (expr.getArg(1) instanceof Constant<?>) {
                constIndex = 1;
                exprIndex = 0;
            }
            if (Collection.class.isAssignableFrom(expr.getArg(constIndex).getType())) {
                @SuppressWarnings("unchecked") //guarded by previous check
                Collection<?> values = ((Constant<? extends Collection<?>>) expr.getArg(constIndex)).getConstant();
                return asDocument(asDBKey(expr, exprIndex), asDocument("$in", values));
            } else {
                Path<?> path = (Path<?>) expr.getArg(exprIndex);
                Constant<?> constant = (Constant<?>) expr.getArg(constIndex);
                return asDocument(asDBKey(expr, exprIndex), convert(path, constant));
            }

        } else if (op == Ops.NOT_IN) {
            int constIndex = 0;
            int exprIndex = 1;
            if (expr.getArg(1) instanceof Constant<?>) {
                constIndex = 1;
                exprIndex = 0;
            }
            if (Collection.class.isAssignableFrom(expr.getArg(constIndex).getType())) {
                @SuppressWarnings("unchecked") //guarded by previous check
                Collection<?> values = ((Constant<? extends Collection<?>>) expr.getArg(constIndex)).getConstant();
                return asDocument(asDBKey(expr, exprIndex), asDocument("$nin", values));
            } else {
                Path<?> path = (Path<?>) expr.getArg(exprIndex);
                Constant<?> constant = (Constant<?>) expr.getArg(constIndex);
                return asDocument(asDBKey(expr, exprIndex), asDocument("$ne", convert(path, constant)));
            }

        } else if (op == Ops.COL_IS_EMPTY) {
            List<Object> list = new ArrayList<Object>(2);
            list.add(asDocument(asDBKey(expr, 0), new ArrayList<Object>()));
            list.add(asDocument(asDBKey(expr, 0), asDocument("$exists", false)));
            return asDocument("$or", list);

        } else if (op == Ops.LT) {
            return asDocument(asDBKey(expr, 0), asDocument("$lt", asDBValue(expr, 1)));

        } else if (op == Ops.GT) {
            return asDocument(asDBKey(expr, 0), asDocument("$gt", asDBValue(expr, 1)));

        } else if (op == Ops.LOE) {
            return asDocument(asDBKey(expr, 0), asDocument("$lte", asDBValue(expr, 1)));

        } else if (op == Ops.GOE) {
            return asDocument(asDBKey(expr, 0), asDocument("$gte", asDBValue(expr, 1)));

        } else if (op == Ops.IS_NULL) {
            return asDocument(asDBKey(expr, 0), asDocument("$exists", false));

        } else if (op == Ops.IS_NOT_NULL) {
            return asDocument(asDBKey(expr, 0), asDocument("$exists", true));

        } else if (op == Ops.CONTAINS_KEY) {
            Path<?> path = (Path<?>) expr.getArg(0);
            Expression<?> key = expr.getArg(1);
            return asDocument(visit(path, context) + "." + key.toString(), asDocument("$exists", true));

        } else if (op == MongodbOps.NEAR) {
            return asDocument(asDBKey(expr, 0), asDocument("$near", asDBValue(expr, 1)));

        } else if (op == MongodbOps.NEAR_SPHERE) {
            return asDocument(asDBKey(expr, 0), asDocument("$nearSphere", asDBValue(expr, 1)));

        } else if (op == MongodbOps.ELEM_MATCH) {
            return asDocument(asDBKey(expr, 0), asDocument("$elemMatch", asDBValue(expr, 1)));
        } else if (op == MongodbOps.NO_MATCH) {
            return new Document("$where", new BsonJavaScript("function() { return false }"));
        }

        throw new UnsupportedOperationException("Illegal operation " + expr);
    }
Serializes the given Querydsl query to a Document query for MongoDB.

@author Mark Paluch
querydsl-mongodb/src/main/java/com/querydsl/mongodb/document/MongodbDocumentSerializer.java