Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...