Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb: Multikey indexing structure?

I'm finding it hard to understand how exactly indexing is done on multikeys in mongodb.

This is what I read about multikeys in mongodb docs on its website:
1) "Creating an index on an array element indexes results in the database indexing each element of the array"
2) "...will index all the tags on the document, and create index entries for "X", "Y" and "Z" for that document."

So what exactly does it mean by index entries for that document? Does each doc remember the entries, in which case searching is gonna be a full table scan? Or is it the same b-tree index of mysql where each index entry will point to multiple documents for each respective occurrence, in which case I'm over thinking too much.

Let's take an example:

obj1 = { 
    name: "Apollo",
    text: "Some text about Apollo moon landings",
    tags: [ "moon", "apollo", "spaceflight", "nasa" ]
}
obj2 = { 
    name: "Atlantis",
    text: "Some text about Atlantis flight missions",
    tags: [ "space", "atlantis", "spaceflight", "nasa" ]
}
db.articles.ensureIndex( { tags : 1 } )

Please help me understand! Thanks, in advance.

like image 416
Ari53nN3o Avatar asked Sep 13 '11 02:09

Ari53nN3o


People also ask

Does MongoDB support Multikey indexes?

MongoDB can use the multikey index to find documents that have 5 at any position in the ratings array.

What is Multikey index in MongoDB?

MongoDB allows to index a field that holds an array value by creating an index key for each element in the array, such type of indexing is called Multikey indexes. It supports efficient queries against array fields.

Which data structure is used by all MongoDB indexes?

A binary tree is the data structure used by an index. In documents, the _id field is a default index which is automatically created by MongoDB and we are not allowed to drop this index.

Does order of indexes matter in MongoDB?

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.


2 Answers

In this case, your index (which is a B-tree) would look like this:

 apollo => [ obj1 ]
 atlantis => [ obj2 ]
 moon => [ obj1 ]
 nasa  => [ obj1, obj2 ]
 space => [ obj2 ]
 spaceflight => [ obj1, obj2 ]

This is just a "regular" B-tree index, except that every document can appear more than once (it appears once for every unique tag value).

like image 58
Thilo Avatar answered Oct 16 '22 12:10

Thilo


I think you misunderstood the difference between Multiindex and Compound indexes:

Compound indexes are user-defined indexes for multiple fields at once. Multykey indexes: MongoDB determine if the field on which the Index is released is an array and create an index for each of the array elements, for example

db.user.ensureIndex({"address.street":1});

In this case and because the target field is an Array the index will store all the items but only once.

I highly recomend you to take a look at this simple articlw that will clarify you doubts about simple imdex types in MongoDB: http://mongodbspain.com/en/2014/01/24/mongodb-indexes-part1/

Regards,

like image 34
CesarTrigo Avatar answered Oct 16 '22 14:10

CesarTrigo