void DocumentSourceMatch::joinMatchWith(intrusive_ptr<DocumentSourceMatch> other) {
    BSONObjBuilder bob;
    BSONArrayBuilder arrBob(bob.subarrayStart("$and"));

    auto addPredicates = [&](const auto& predicates) {
        if (predicates.isEmpty()) {
            arrBob.append(predicates);
        }

        for (auto&& pred : predicates) {
            // If 'pred' is an $and, add its children directly to the new top-level $and to avoid
            // nesting $and's. Otherwise, add 'pred' itself as a child.
            if (pred.fieldNameStringData() == "$and") {
                for (auto& child : pred.Array()) {
                    arrBob.append(child);
                }
            } else {
                BSONObjBuilder childBob(arrBob.subobjStart());
                childBob.append(pred);
            }
        }
    };

    addPredicates(_predicate);
    addPredicates(other->_predicate);

    arrBob.doneFast();
    rebuild(bob.obj());
}
void DocumentSourceMatch::joinMatchWith(intrusive_ptr<DocumentSourceMatch> other) {
    BSONObjBuilder bob;
    BSONArrayBuilder arrBob(bob.subarrayStart("$and"));

    auto addPredicates = [&](const auto& predicates) {
        if (predicates.isEmpty()) {
            arrBob.append(predicates);
        }

        for (auto&& pred : predicates) {
             
             
            if (pred.fieldNameStringData() == "$and") {
                for (auto& child : pred.Array()) {
                    arrBob.append(child);
                }
            } else {
                BSONObjBuilder childBob(arrBob.subobjStart());
                childBob.append(pred);
            }
        }
    };

    addPredicates(_predicate);
    addPredicates(other->_predicate);

    arrBob.doneFast();
    rebuild(bob.obj());
}