Couchbase Update query divide

Viewed 187

I am trying to update the document using the UPDATE query statement on the couchbase. EX) UPDATE Users SET cityIndex = 1 where Users.city= "NewYork";

There was so much data that I wanted to divide 3,000 to 4,000 and proceed with the UPDATE. How should I proceed? There is PRIMARY INDEX.

3 Answers

The Eventing Function method that vsr alluded too is quite simple (7 lines sans comments) and you run it as a one-off point tool deploying it from Everything. Note there is no need for any index for this to work.

// To run configure the settings for this Function, UpdateAllCityIndex, as follows:
//
// Version 7.0+
//   "Listen to Location"
//     bulk.data.yourcollection
//   "Eventing Storage"
//     rr100.eventing.metadata
//   Binding(s)
//    1. "binding type", "alias name...", "bucket.scope.collection", "Access"
// ---------------------------------------------------------------------------
//       "bucket alias", "src_col",       "bulk.data.Users",         "read and write"
//
// Version 6.X
//   "Source Bucket"
//     yourbucket
//   "MetaData Bucket"
//     metadata
//   Binding(s)
//    1. "binding type", "alias name...", "bucket",     "Access"
// ---------------------------------------------------------------------------
//       "bucket alias", "src_col",       "Users",   "read and write"
//
// For more performance set the workers to the number of physical cores

function OnUpdate(doc, meta) {
    // only process documents with the city field
    if (!doc.city) return;
    
    // only update New York if cityIndex isn't already 1 or does not exist
    if (  doc.city === "NewYork" && (!doc.cityIndex || doc.cityIndex !== 1 )) {
        doc.cityIndex = 1;
        // write back the updated doc via the alias
        src_col[meta.id] = doc;
    }
}

Using a primary index, you can issues multiple queries on a (presumably stable primary index) and iterate over it. Little bit more complicated, but generalized.

rq is the bucket, s is the scope, t1 is the collection.

create collection rq.s.t1; create primary index on rq.s.t1;

First query:

UPDATE rq.s.t1 USE KEYS [( SELECT META().id FROM rq.s.t1 ORDER BY META().id LIMIT 10)] SET x = 1 RETURNING MAX(META().id);

Second to N query until you're done (nothing gets returned): Take the max value of meta().id from the previous query (see the WHERE clause)

UPDATE rq.s.t1 USE KEYS [( SELECT RAW META().id FROM rq.s.t1 WHERE META().id > "007dd444-fa39-498f-b070-6cd0d41abe3d" ORDER BY META().id LIMIT 10)] SET x = 1 RETURNING META().id;

You can optimize this loop by setting the initial meta().id to compare against "".

Related