How add a group by to the query used by the media library?

I found a solution; use pre_get_posts:

add_action( 'pre_get_posts', 'modify_attachments' );

function modify_attachments( $query ) {
    if ( is_admin() && strpos( $_SERVER[ 'REQUEST_URI' ], 'admin-ajax.php' ) !== false ) {      
        add_filter( 'posts_groupby', 'group_attachments' );
    }
 return $query;
}

function group_attachments( $groupby ) {  
    $groupby .= " guid";
    return $groupby;        
}