Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does field type matter in a MongoDB index?

Here are two examples of a document structure in MongoDB.

{  
 UserId: "123",  
 UserName: "Usain Bolt"  
}



{  
 UserId: NumberLong(123),  
 UserName: "Usain Bolt"  
}

If I were to create an index on UserID, would there be a difference in find query performance between the above two examples?

like image 224
Rajan Avatar asked Aug 19 '12 05:08

Rajan


People also ask

Does MongoDB index all fields?

MongoDB provides complete support for indexes on any field in a collection of documents. By default, all collections have an index on the _id field, and applications and users may add additional indexes to support important queries and operations. This document describes ascending/descending indexes on a single field.

Does MongoDB index order matter?

Indexes store references to fields in either ascending ( 1 ) or descending ( -1 ) sort order. For single-field indexes, the sort order of keys doesn't matter because MongoDB can traverse the index in either direction.

How does MongoDB decide which index to use?

MongoDB uses multikey indexes to index the content stored in arrays. If you index a field that holds an array value, MongoDB creates separate index entries for every element of the array. These multikey indexes allow queries to select documents that contain arrays by matching on element or elements of the arrays.

What is true about indexes in MongoDB?

The index stores the value of a specific field or set of fields, ordered by the value of the field. The ordering of the index entries supports efficient equality matches and range-based query operations. In addition, MongoDB can return sorted results by using the ordering in the index.


2 Answers

I don't have a specifically accurate answer on the matter, but here is my understanding:

Indexes

Indexed fields may be of any type, including (embedded) documents

Indexes are mostly likely hashed, regardless of the type, to be able to index the same way. If there were specific considerations to be observed for different types, the standard mongodb docs would make that distinction.

The answer given to a similar question here refers to code samples from mongodb, that suggests the index comparisons are pretty much the same regarding types, but probably performance geared towards the size of the index.

like image 169
jdi Avatar answered Sep 27 '22 23:09

jdi


One thing to note about strings versus numbers when it comes to indexes is that the order for a number in a sort is generally well understood. However when you use a string Lexicographical Order will be used instead, so you need to be aware of the differences there.

Also, to follow up on the size angle mentioned by jdi, the string can be arbitrarily long, though that can be controlled of course, a long, which will be stored as the BSON double type will always be 8 bytes.

like image 43
Adam Comerford Avatar answered Sep 27 '22 22:09

Adam Comerford