Académique Documents
Professionnel Documents
Culture Documents
, 5)
[10,
[-
, 5) buckets
[5, 7)
[7, 9)
[9, 10)
[10,
) buckets
{...} {...} {...} {...} {...} {...} {...} {...} {...} {...} {...}
Creating Indexes
An
index
on
_id
is
automatic. For
more
use
ensureIndex: db.blogs.ensureIndex({author:
1}) 1
=
ascending -1
=
descending
Compound Indexes
db.blogs.save({
author:
"James",
ts:
new
Date()
... }); db.blogs.ensureIndex({author:
1,
ts:
-1})
Unique Indexes
db.blogs.save({
author:
"James",
title:
"My
first
blog"
... });
db.blogs.ensureIndex({title:
1},
{unique:
true})
Multikeys
db.blogs.save({
title:
"My
Second
blog",
tags:
["mongodb",
"NoSQL"] }); db.blogs.ensureIndex({tags:
1}) db.blogs.find({tags:
"NoSQL"})
Covered Indexes
From 1.8.0 Query resolved in index only Need to exclude _id from items projected
db.blogs.save({
author:
"James",
title:
"My
first
blog" });
db.blogs.ensureIndex({author:
1}) db.blogs.find({author:
"James"},
{author:
1,
_id:0}))
Sparse Indexes
From 1.8.0 Key value included if and only if the value is present Reduces size of index Limited to a single field
db.blogs.ensureIndex({loc:
1},
{sparse:
true}) //
loc
key
stored
for
Ben
&
Sarah
only db.blogs.save({author:
"Jim"}) db.blogs.save({author:
"Ben",
loc:
null}) db.blogs.save({author:
"Sarah",
loc:
"CA"})
New in 2.0.0 25% smaller, 25% faster All new indexes created in new format Index keys increase to 1024 bytes (from 812 bytes) Old indexes retained "as is"
>
db.blogs.ensureIndex({tags:-1}); >
db.blogs.getIndexes() ...
{
"v"
:
1,
"key"
:
{
"tags"
:
-1
} ... //
Can
still
create
indexes
in
older
format >
db.blogs.ensureIndex({tags:-1},
{v:0});
Indexes in 2.0
Repair database
>
db.repairDatabase();
Compact command
>
db.blogs.runCommand(
"compact"
)
Geospatial
Geo hash stored in B-Tree First two values indexed
db.blogs.save({
loc:
{
long:
40.739037,
lat:
40.739037
} }); db.blogs.save({
loc:
[40.739037,
40.739037] }); db.blogs.ensureIndex({"loc":
"2d"})
Geospatial in 2.0
Multi-location indexes for a single document $near may return the document for each index match $within will return a document once and once only
>
db.places.insert({
addresses
:
[
{
name
:
"Home",
loc
:
[55.5,
42.3]
},
{
name
:
"Work",
loc
:
[32.3,
44.2]
}
]
}) >
db.places.ensureIndex({
"addresses.loc"
:
"2d"
}) >
db.places.insert({
lastSeenAt
:
[
{
x
:
45.3,
y
:
32.2
},
[54.2,
32.3],
{
lon
:
44.2,
lat
:
38.2
}
]
}) >
db.places.ensureIndex({
"lastSeenAt"
:
"2d"
})
Picking an index
db.c.ensureIndex({x:1}) db.c.ensureIndex({y:-1}) db.c.find({x:
10,
y:
foo})
scan terminated
index
on
x
index on y
remember
{_id:4,x:6} {_id:1,x:6}
{_id:5,x:6}
Equality match
db.c.find({x:6}).explain() {
"cursor"
:
"BtreeCursor
x_1",
"nscanned"
:
3,
"nscannedObjects"
:
3,
"n"
:
3,
"millis"
:
1,
"nYields"
:
0,
"nChunkSkips"
:
0,
"isMultiKey"
:
false,
"indexOnly"
:
false,
"indexBounds"
:
{
"x"
:
[
[
6,
6
]
]
} }
Equality match
"indexBounds"
:
{
"x"
:
[
[
6,
6
]
] }
Equality match
"nscanned"
:
3,
"nscannedObjects"
:
3,
"n"
:
3,
Number of matching documents is 3
Equality match
db.c.find({x:
6})
5 2 1 3 4 6 6 6 9
{y:4,x:6} {y:1,x:6}
{y:5,x:6}
Range match
db.c.ensureIndex({x:
1}) db.c.find({x:{$gte:4,
$lte:7}})
Range match
db.c.find(
{x:{$gte:4,$lte:7}}
).explain() {
"cursor"
:
"BtreeCursor
x_1",
"nscanned"
:
4,
"nscannedObjects"
:
4,
"n"
:
4,
"millis"
:
1,
"nYields"
:
0,
"nChunkSkips"
:
0,
"isMultiKey"
:
false,
"indexOnly"
:
false,
"indexBounds"
:
{
"x"
:
[
[
4,
7
]
]
} }
Range match
"indexBounds"
:
{
"x"
:
[
[
4,
7
]
]
Range match
"nscanned"
:
4,
"nscannedObjects"
:
4,
"n"
:
4,
Number of matching documents is 4
Range match 5 2 1 3 4 6 7 8 9
Mongostat
insert
query
update
delete
getmore
command
flushes
mapped
vsize
res
locked
%
idx
miss
%
5345
0
0
0
0
1
0
8.75g
11.1g
132m
1
2
230
0
0
0
0
1
0
8.75g
11.1g
132m
1
7
1740
20
0
0
0
2
0
8.75g
11.1g
132m
3
19
0
120
0
0
0
1
0
8.75g
11.1g
132m
0
17
0
117
0
0
0
1
0
8.75g
11.1g
132m
0
16
0
119
0
0
0
1
0
8.75g
11.1g
132m
0
17
download at mongodb.org
Were Hiring !
alvin@10gen.com
conferences,
appearances,
and
meetups
http://www.10gen.com/events
http://bit.ly/mongo@
http://linkd.in/joinmongo