Reading Indexes

You use Store.indexIterator() to retrieve table rows using a table's indexes. Just as when you use TableIterator to read table rows using a table's primary key(s), when reading using indexes you can set options such as field ranges, traversal direction, and so forth. By default, index scans return entries in forward order.

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)
) 
CREATE INDEX DoB ON myTable (dateOfBirth) 

This creates an index named DoB for table myTable based on the value of the dateOfBirth field. To scan through that index, do the following:

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

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

   store.indexIterator('myTable', 'DoB', {},
           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(); 

In the previous example, the code examines every row indexed by the DoB index. A more likely, and useful, example in this case would be to limit the rows returned through the use of a field range. You do that by constructing a FieldRange object. When you do this, you must specify the field to base the range on. Recall that an index can be based on more than one table field, so the field name you give the object must be one of the indexed fields.

For example, if the rows hold dates in the form of yyyy-mm-dd, you could retrieve all the people born in the month of May, 1994 in the following way. This index only examines one field, dateOfBirth, so we give that field name to the FieldRange object:

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

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

   var fieldRange = new nosqldb.Types.FieldRange(
                "dateOfBirth", "1994-05-01", true,
                "1994-05-30", true);

   store.indexIterator('myTable', 'DoB', {
            fieldRange: fieldRange
           },
           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();