问题
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.
回答1:
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).
回答2:
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,
来源:https://stackoverflow.com/questions/7396219/mongodb-multikey-indexing-structure