Include one more column or type in existing document

Hello Everyone,

Currently I am using document structure with bucket name “ctrl-test”
{
“productId”: 77,
“countryCode”: “TT”,
“languageIsoCode”: “en”,
“storeType”: “TEST”
}

I want to update all the existing documents available in bucket “ctrl-test” With following

{
“productId”: 77,
“countryCode”: “TT”,
“languageIsoCode”: “en”,
“storeType”: “TEST”
“storeTypes”: [“TEST”,“OPS”]
}
in above document i am adding one more column “storeTypes”: [“TEST”,“OPS”]

Please suggest me with some script which will help .

Thanks

UPDATE `ctrl-test` SET `storeTypes` = ["TEST","OPS"]

Will do it if you have a primary index (primary index not recommended in production); otherwise add a filter on a column you have indexed, e.g. if “productId” is indexed:

UPDATE `ctrl-test` SET `storeTypes` = ["TEST","OPS"] WHERE `productId` IS NOT MISSING

If you’re looking to make storeTypes be the existing value of storeType plus the value “OPS”:

UPDATE `ctrl-test` SET `storeTypes` = [`storeType`,"OPS"]

HTH.

(Ref: https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/update.html)

Hi @sumitbnc30,

You can also use the Eventing Service as a one off point tool, in some cases you might have a lot of documents 1B to modify which makes using N1QL a bit harder to use (you would need LIMIT and OFFSET and multiple N1QL runs).

An Eventing Function would be as follows:

function OnUpdate(doc, meta) {
    if (!doc.storeTypes) return;
    doc.storeTypes = [“TEST”,“OPS”];
    // This is a bucket binding i.t.. src_col is read-write alias to 
    // the source keypace the function is listening to.
    sec_bkt[meta.id] = doc;
}

Best

Jon Strabala
Principal Product Manager - Server‌

Thanks . It worked for me .