Indexing in MongoDB
Important to know about indexes:
1.) _id is the default index in all collections.
See..lets see all indexes in my score collection
> db.score.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "movie.score"
}
]
>
Here "v" : 2, :: is version, internal mongo housekeeping
"name" : "_id_", ::::: is the name of the index
"ns" : "movie.score" ::::::::::::is the namespace, fully qualified name of the collection.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2.) EnsureIndex : how to ensure that you have index on separate field.
lets see one collection what data is holds and where we can create index
> db.score.findOne()
{
"_id" : 0,
"name" : "aimee Zank",
"scores" : [
{
"score" : 1.463179736705023,
"type" : "exam"
},
{
"score" : 11.78273309957772,
"type" : "quiz"
},
{
"score" : 35.8740349954354,
"type" : "homework"
}
]
}
1.) _id is the default index in all collections.
See..lets see all indexes in my score collection
> db.score.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "movie.score"
}
]
>
Here "v" : 2, :: is version, internal mongo housekeeping
"key" : {
"_id" : 1 -------- key is actual index description, it says index is based on the field _ID and that it is holding the value in ascending order."name" : "_id_", ::::: is the name of the index
"ns" : "movie.score" ::::::::::::is the namespace, fully qualified name of the collection.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2.) EnsureIndex : how to ensure that you have index on separate field.
lets see one collection what data is holds and where we can create index
> db.score.findOne()
{
"_id" : 0,
"name" : "aimee Zank",
"scores" : [
{
"score" : 1.463179736705023,
"type" : "exam"
},
{
"score" : 11.78273309957772,
"type" : "quiz"
},
{
"score" : 35.8740349954354,
"type" : "homework"
}
]
}
Now lets create an index on score
> db.score.ensureIndex({scores:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
>
> db.score.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "movie.score"
},
{
"v" : 2,
"key" : {
"scores" : 1
},
"name" : "scores_1",
"ns" : "movie.score"
}
]
To drop the index
> db.score.dropIndex('scores_1')
{ "nIndexesWas" : 2, "ok" : 1 }
> db.score.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "movie.score"
}
]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Index with Nested document:
> db.score.findOne()
{
"_id" : 0,
"name" : "aimee Zank",
"scores" : [
{
"score" : 1.463179736705023,
"type" : "exam"
},
{
"score" : 11.78273309957772,
"type" : "quiz"
},
{
"score" : 35.8740349954354,
"type" : "homework"
}
]
}
>
Let create an index in TYPE field in the SCORES document
> db.score.ensureIndex({"scores.type":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.score.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "movie.score"
},
{
"v" : 2,
"key" : {
"scores.type" : 1
},
"name" : "scores.type_1",
"ns" : "movie.score"
}
]
+++++++++++++++++++++++++++++++++++++++++++++++++=++++++
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home