Database Reference
In-Depth Information
important points of commonality for the document in the MongoDB
collection.
Indexes in MongoDB are defined using the
db.collection.createIndex function and a JSON object that defines
the direction of sorting for each field contained within the index:
> db.first.ensureIndex({foo:1,bar:-1});
> db.first.stats()
{
"ns" : "mine.first",
"count" : 0,
"size" : 0,
"storageSize" : 8192,
"numExtents" : 1,
"nindexes" : 2,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 0,
"totalIndexSize" : 16352,
"indexSizes" : {
"_id_" : 8176,
"foo_1_bar_-1" : 8176
},
"ok" : 1
}
This command creates an index on the first collection created earlier on
two fields: foo and bar . The foo field is sorted in an ascending natural
ordering for its contents (numerical for numerical values and lexical for
string values), whereas bar is sorted in descending order. Using
dot-notation, it is possible to index on nested fields in a document. For
example, the following defines an index on the zip subfield of the addr
object in a document:
> db.first.ensureIndex({"addr.zip":1});
> db.first.getIndexes();
[
Search WWH ::




Custom Search