Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Index on different types

We can have { data: "hello" }, { data: 123 } in the same collection and even create a index on it. I'm curious how does mongodb manage the index behind the scene. We can't create single B-tree on different types. Right? However, I did getIndexes to see if another index is created but only one index is created.

like image 373
Alice Avatar asked Feb 03 '11 14:02

Alice


1 Answers

There's no problem having two types in the same index. Each key within the index includes the type.

When you query, only objects matching the type you query on will be returned.

So if you query for {data: "hello"}, only strings will be returned, etc.

like image 107
Kyle Banker Avatar answered Nov 14 '22 10:11

Kyle Banker