Couchbase eventing for the collection change

We are planning to migrate to a set of collections for the default collection. We have written some code in the eventing function to copy documents from one collection to another, but the attachments are not being copied correctly.

if (doc.type === "test") {
    test_col[meta.id] = doc;
}
if (doc.type === "test_2") {
    test_2col[meta.id] = doc;
}

Here, test_col and test_2col belong to the same bucket. We are listening to the default bucket and moving documents to specific collections based on their type, but the documents are being copied while the attachments are getting corrupted.

but the documents are being copied while the attachments are getting corrupted

This just sounds wrong are you saying that Eventing is causing the corruption if not where is the corruption coming from.

What I don’t see is the alias are you actually writing to different collections. I would be helpful to attach your full exported Eventing Function.

Next I have a blog series on doing exactly what you are asking you should look at the two parts:

Finally if you are worried about corruption you can actually use the crc64 function to verify the the source document and the destination document are exactly the same. So after your function has done the copy you expect you can just verify things as follows

    var s_doc = src_collection[meta.id];
    var d_doc = dst_collection[meta.id];

    // Compute crc64 checksums for the original doc, source doc, and destination doc
    var crc64_original = crc64(JSON.stringify(doc));
    var crc64_source = crc64(JSON.stringify(s_doc));
    var crc64_destination = crc64(JSON.stringify(d_doc));

    // Compare checksums to check if they are the same
    if (crc64_original === crc64_source && crc64_source === crc64_destination) {
        // log('Documents match');
    } else {
        log('Documents do not match');
        log('Original checksum: ', crc64_original);
        log('Source checksum: ', crc64_source);
        log('Destination checksum: ', crc64_destination);
    }