Skip to content

Instantly share code, notes, and snippets.

@mbalex99
Last active October 16, 2018 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbalex99/7b8e147ad41b4b51ba98e6ee57e16ab1 to your computer and use it in GitHub Desktop.
Save mbalex99/7b8e147ad41b4b51ba98e6ee57e16ab1 to your computer and use it in GitHub Desktop.
Idea for Secondary Indexes

If we had documents in the cars collection like this:

[

    {

        "_id": "123abc",

        "color": "red",

        "make": "Honda"

    },

    {

        "_id": "456xyz",

        "color": "blue",

        "make": "Toyota"

    }

]

Primary Indices will look like this


Key Pattern: ${collectionName}::${property}=${propertyValue}::${primaryKeyValue}

So a concrete example from the above will look like this:


cars::_id="123abc"::123abc => {

        "_id": "123abc",

        "color": "red",

        "make": "Honda"

}

cars::_id="456xyz"::456xyz => {

        "_id": "456xyz",

        "color": "blue",

        "make": "Toyota"

}

Then if we create a secondary index on the "make", we will add two more keys


cars::_id="123abc"::123abc => {...}

cars::_id="456xyz"::456xy  => {...}

cars::make="Toyota"::123abc => Ø

cars::make="Honda"::456xy => Ø

Secondary index values are already stored in the key. So no necessity in putting anything in the value.

So when a query comes in looking for "cars.make == 'Honda",

it'll loop over all the secondary indices first sees which values match,

pluck their primary keys and do another lap around the collection to get the actual JSON values

@AndyDentFree
Copy link

One thought triggered by our other discussions - depending on other iterations and searching, building a key that starts with make and ends with _id may want to have other fields in the middle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment