【MongoDB】The description of index(一),mongodbdescription
From this blog, we start to talk about the index in mongo Database, which is similar to the traditional database. Generally speaking, if the index need to be created in the traditional database, so does MongoDB. In MongoDB ,the field '_id ' has been set index by default and this index is so special that it cannot be deleted except for Capped Collections.
Before studying the index, now we create 10000 test records as follows.
1 Create index
In mongodb, using the function ensureIndex() to create tne index, for example:
db.test.ensureIndex({name : 1}) means to create index for name
2. Use index
generally speaking, there is five way to create index based on the practice condition.
2.1 Common Index
query the result before creating index and after.
Some explanation of result field:
Cursor: value [BasicCursor or BtreeCursor], the later explain this query has used index.
nscanned: the number of index of having scan.
n : return the number of document, namely return the line
millis: the total time of finishing this query, unit millinus seconds.
indexBounds: if not null, it will describe the index item,
the nature of the goods與description of the goods是不是一回事
都是貨描,但是前者側重於貨物本身的屬性,後者包含封裝等所有與貨物有關的內容。
說明一下indexOf()方法的執行原理
indexOf(),是string對象的一個方法,被重載過4次,每一次的用法為:
int indexOf(int ch)
返回指定字元在此字串中第一次出現處的索引。
int indexOf(int ch, int fromIndex)
從指定的索引開始搜尋,返回在此字串中第一次出現指定字元處的索引。
int indexOf(String str)
返回第一次出現的指定子字串在此字串中的索引。
int indexOf(String str, int fromIndex)
從指定的索引處開始,返回第一次出現的指定子字串在此字串中的索引。
這段摘自jdk1.5API
原始碼:
public int indexOf(int ch, int fromIndex) {
int max = offset + count;
char v[] = value;
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= count) {
// Note: fromIndex might be near -1>>>1.
return -1;
}
int i = offset + fromIndex;
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// handle most cases here (ch is a BMP code point or a
// negative value (invalid code point))
for (; i < max ; i++) {
if (v[i] == ch) {
return i - offset;
}
}
return -1;
}
if (ch <= Character.MAX_CODE_POINT) {
// handle supplementary characters here
char[] surrogates = Character.toChars(ch);
for (; i < max; i++) {
if (v[i] == surrogates[0]) {
if (i + 1 == max) {
break;
}
if (v[i+1] == surrogates[1]) {
return i - offset;
}
}
}
}
return -1;
}...餘下全文>>