Specifying Field Ranges

When performing multi-key operations in the store, you can specify a range of rows to operate upon. You do this using a FieldRange object, which is accepted by any of the methods which perform bulk reads. This object is used to restrict the selected rows to those matching a range of field values.

For example, suppose you defined a table like this:

CREATE TABLE myTable (
    surname STRING,
    familiarName STRING,
    userID STRING,
    phonenumber STRING,
    address STRING,
    email STRING,
    dateOfBirth STRING,
    PRIMARY KEY (SHARD(surname, familiarName), userID)
) 

The surname contains a person's family name, such as Smith. The familiarName contains their common name, such as Bob, Patricia, Robert, and so forth.

Given this, you could perform operations for all the rows related to users with a surname of Smith, but we can limit the result set to just those users with familiar names that fall alphabetically between Bob and Patricia by specifying a field range.

A FieldRange is created using the FieldRange class, which you provide to the method you are using to perform the multi-read operation using the Options object. This class requires the name of the primary key field for which you want to set the range, as well the range values, including whether they are inclusive.

In this case, we will define the start of the key range using the string "Bob" and the end of the key range to be "Patricia". Both ends of the key range will be inclusive.

In this example, we use TableIterator, but we could just as easily use this range on any multi-row read operation, such as the Store.multiGet() or Store.multiGetKeys() methods.

...
// Store handle configuration and open skipped for brevity
...

// Open the store with the specified configuration
var store = nosqldb.createStore(configuration);

store.on('open', function () {
   console.log('Store opened');

   // Using a partial primary key for
   // this iteration
   var key = {surname: "Smith"};

   var fieldRange = new nosqldb.Types.FieldRange(
                "familiarName", "Bob", true,
                "Patricia", true);

   store.tableIterator('myTable', key, { 
            fieldRange: fieldRange,
            // Must be UNORDERED because we are iterating
            // using a partial primary key.
            direction: nosqldb.Types.Direction.UNORDERED
           },
           function (err, iterator) {
                if (err)
                    throw err;
                else {

                    // Configure Iterator event done
                    iterator.on('done', function() {
                        store.close();
                    });

                    console.log("Retrieved rows:");

                    iterator.forEach(function (err, returnRow) {
                        if (err)
                            throw err;
                        else
                            console.log(returnRow.row);
                            console.log('\n');
                    });
                }
           });
}).on('close', function() {
    console.log('Store closed.');
}).on('error', function(error) {
    console.log(error);
});
store.open();