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.
MongoDB can use the multikey index to find documents that have 5 at any position in the ratings array.
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.
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.
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.
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).
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,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With