Skip to main content

Indexing NamedUserCollections

In this lesson, you will learn about four types of indexes:

Single Field Indexes Compound Indexes Text Indexes In the following lesson you will download code and run some tests to understand how to create indexes, and even do some querying, though the sample data you will create will not be large enough to see noticeable differences in the query time.

For each index description below the following type of Related Items will be used to illustrate the benefit of the index:

{   "category": "Pump",   "type": "Impulse",   "count": 10,   "info": {      "uniqueId": "PMP-IMP-100",      "description": "This style of pump is used for..."   }}

Single Field Indexes#

If you will be frequently querying your data based on one of the fields on that data, you should create an index on that field. For instance, in the example above, perhaps you will be querying your data based on the category field. You'd want to create a Single Field Index on category to make your queries as fast as possible.

You may create multiple Single Field Indexes on one NamedUserCollection, so if you also will be querying frequently on the info.uniqueId property you can also create a second index on it.

In the case of multiple Single Field Indexes, you would create more than one index each with only one field in its definition. They would appear in Twinit like this:

[   {      "v": 2,      "name": "category-ASC",      "key": {         "category": 1      },      "collectionName": "singleindex_PwdrhOJW1s_V1"   },   {      "v": 2,      "name": "uniqueid-DESC",      "key": {         "info.uniqueId": -1      },      "collectionName": "singleindex_PwdrhOJW1s_V1"   }]

Notice that there are two separate indexes each with a single field.

Compound Indexes#

Compound Indexes are helpful when you are frequently querying the same combination of fields on your Related Items. For instance, in the example above, perhaps you will be querying your data based on the category and type fields together. You'd want to create a Compound Index on category and type to make your queries as fast as possible.

This is not the same as two Single Field Indexes. A Compound Index takes into account each field and the order in which those fields appear in the index definition, as well as the sort you give them.

A Compound Index on category and type would appear in Twinit like this:

[   {      "v": 2,      "name": "category-type-ASC",      "key": {         "category": 1,         "type": 1      },      "collectionName": "compindex_PwdrhOJW1s_V1"   }]

This Compound Index will first index category ascending and then for each category index the type ascending. If you query your data based on the combination of category and type, and sort the results ascending for each field then this Compound Index will be more efficient than if you were to query the data without it. However, if you query by category ascending but type descending this Compound Index will not be useful.

A Compound Index can also serve as a Single Field Index or Compound Index on any of the prefix fields. In the example of the Compound Index above, category is a prefix. So searching by category alone would also benefit from the Compound Index. If there was a third field added to the index after type (maybe count), then both category and type would be a prefix, and searches based off only those two fields would also benefit.

Text Indexes#

Text Indexes allow you to specify fields in your data containing string values that you want to be able to search based on the content of the string. For instance, in the examples above, you want to find all Related Items with the word "pump" in their description field or the "PMP" in their uniqueId field. You can do this by creating a Text Index.

When a Text Index is created it will tokenize the string values on your Related Items for searching. Values will be tokenized based on white spaces, dashes, hyphens, quotation marks, most punctuation, and pattern syntax characters such as "<<" or ">>".

Then when a text search is performed, the tokenized values will be used to return results.

Sample String: "PMP-IMP.Lower Level\<\<EAST WING>>"

All these searches would return the Related Item with the Sample String value in an indexed field:

  • PMP
  • pmp
  • IMP
  • Lower
  • Level
  • "Lower Level"
  • EAST
  • east
  • EAST WING

These searches would not return the Related Item:

  • "Lower EAST"
  • "PMP Level"
  • WEST

When creating a text index you can create it with multiple fields, however there can only be one text index per NamedUserCollection.

A Text Index on uniqueId and description would appear in Twinit like this:

[   {      "v": 2,      "name": "uniqueid-desc-text",      "key": {         "info.uniqueId": "text",         "info.description": "text"      },      "collectionName": "textindex_PwdrhOJW1s_V1"   }]

When searching based off a text index your query will use the $text and $search operators like this:

query: { $text: { $search: "PMP" } }

Wildcard Text Indexes#

If your data is highly unstructured or you are unsure of what fields to index for text searching, a Wildcard Text Index will create a text index on every field on your Related Items that contain string data. Wildcard Text Indexes should be used carefully, as the index size could grow very quickly.

In the example above, a Wildcard Text Index would index the category, type, uniqueId, and description fields on every Related Item.

A Wildcard Text Index uses the "$**" keyword and would appear in Twinit like this:

[   {      "v": 2,      "name": "wildcard-text",      "key": {         "$**": "text"      },      "collectionName": "textindex_PwdrhOJW1s_V1"   }]

Hands On#

  1. Download INT04 - NamedUserCollection Indexing.zip and extract its contents
  2. Follow the steps in the file