For JSON data/document

Secondary key or indexes are created once the table is created or opened. This can only be done for Wide_Table type. Please see bangdb_table_type for more info. Using addIndex() method we can do this. Again we use table_env to set the property of the index.

Here are methods to do the same:

int addIndex(const char *idxName, table_env *tenv);

The above is the generic method to add index, however for simplicity there are two helper methods are provided to create, one index for string key and other for nominal key. We can use these two assuming all default setting is acceptable or we can use the generic one which gives us more flexibility.

The helper ones are following:

Always creates index with following property

  • log = off
int addIndex_str(const char *idxName, int idx_size, bool allowDuplicates);
int addIndex_num(const char *idxName, bool allowDuplicates);

Now, let's create index for sting as follows on a field "name"

tbl->addIndex_str("name", 32, true);

We just created an index for field “name”, with max size as 32 and allowed duplicate entries for the same. Now, when we put document into the table, db will add index for the name column as well.

  const char * doc = "{
  "name": "sachin",
  "org": "bangdb",
  "address": {
     "city": "Bangalore",
     "state": "KA"
  "fav_quote": "We build too many walls and not enough bridges"

We should check for error returned by the API put_doc, but ignored for brevity. Here, we did not provide explicit primary key, therefore db will add "pk" field in the doc and use it as primary key. This "pk" is timestamp. Also, we didn't specify if we wanted reverse index to be created or now, later we will see this as well.

Now let's scan using the index

const char *query = "{"query":[{"key":"name", "cmp_op":4, "val":"sachin"}]}";

  // this query is to find all docs where name = "sachin", 4 is EQ enum value for scan_operator enum.
  // we can also create this using dataQuery as follows;
  dataQuery dq;
  dq.addFilter("name", EQ, "sachin", JO_INVALID);
  const char *query = dq.getQuery();
  We can keep adding like this to create complex query
  resultset *rs = NULL;
  scan_filter sf;
      rs = tbl->scan_doc(rs, NULL, NULL, query, &sf);
          // keys and vals