MongoDB Single Field Indexes
Description
All the indexes in MongoDB are B-tree indexes, which supports equality matches and range queries. Internally the index arranged items in an order, sorted by the value of the index field. The ordering of index allows MongoDB to return sorted results using the order of documents in the index.
MongoDB provides indexes on any field in a collection of documents. MongoDB supports on either a single field or multiple fields depending on the operations a user needs. The default index for collections have mongoDB on the _id field, the additional indexes can be added by the user to support important queries and operations.
Here is the documents in the collection 'empinfo'.
{
"_id" : ObjectId("548eb3a051ca033c1c01054d"),
"emp_id" : "SALS",
"emp_name" : "Pat",
"designation" : "clerk",
"department" : "sales"
}
{
"_id" : ObjectId("548eb3a051ca033c1c01054e"),
"emp_id" : "OFFC",
"emp_name" : "Jons",
"designation" : "peon",
"department" : "sales"
}
{
"_id" : ObjectId("548eb3a051ca033c1c01054f"),
"emp_id" : "CLC",
"emp_name" : "Tnit",
"designation" : "president",
"department" : "sales"
}
The following command create the index on emp_id column.
> db.empinfo.ensureIndex( { "emp_id" : 1 } );
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
If you want to see the list of created index, here is the following command.
> db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "empinfo.empinfo" }
{ "v" : 1, "key" : { "emp_id" : 1 }, "name" : "emp_id_1", "ns" : "empinfo.empinfo" }
Indexes on Embedded Fields
The indexes can be created on fields embedded in sub-documents, as you can index top-level fields in documents.
There is a difference between Indexes on embedded fields and indexes on sub-documents. The indexes on subdocument include the full content up to the maximum index size whereas indexes on embedded fields allow the user to use a “dot notation,” to self-searching into sub-documents.
Consider the following collection named 'empinfo' that contains the documents.
{
"_id" : ObjectId("548fe6d898fd7de25e8d3aa9"),
"emp_id" : "SALS",
"emp_name" : "Pat",
"address" : [
{
"street" : "Court Street",
"city" : "London",
"zip" : "5698722"
}
]
}
{
"_id" : ObjectId("548fe6d898fd7de25e8d3aaa"),
"emp_id" : "OFFC",
"emp_name" : "Jones",
"address" : [
{
"street" : "Church Avenue",
"city" : "Paris",
"zip" : "632902"
}
]
}
The following command create the index on address.zip field, using the following specification:
> db.empinfo.ensureIndex( { "address.zip": 1 } );
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
If you want to see the created index, see the following command.
> db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "empinfo.empinfo" }
{ "v" : 1, "key" : { "address.zip" : 1 }, "name" : "address.zip_1", "ns" : "empinfo.empinfo" }
>
Indexes on Subdocuments
Index can also be created on subdocument.
Here in the below a sample collection named 'empinfo' contain a field address within its documents.
{
"_id" : ObjectId("54901b4b4b71cc67737765a5"),
"emp_id" : "SALS",
"emp_name" : "Pat",
"address" : {
"street" : "Court Street",
"city" : "London",
"zip" : "5698722"
}
}
{
"_id" : ObjectId("54901b4b4b71cc67737765a6"),
"emp_id" : "MARK",
"emp_name" : "James",
"address" : {
"street" : "Church Avenue",
"city" : "Paris",
"zip" : "532460"
}
}
The address field is a subdocument, containing the embedded fields city and zip. The following command creates an index on the address field as a whole.
> db.empinfo.ensureIndex( { address: 1 } );
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
If you want to lists the creating index, here is the command below.
> db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "empinfo.empinfo" }
{ "v" : 1, "key" : { "address" : 1 }, "name" : "address_1", "ns" : "empinfo.empinfo" }
The following query can use the index on the address field :
> db.empinfo.find( { address: { street: "Court Street", city: "London",zip:"5698722" } } ).pretty();
{
"_id" : ObjectId("54901b4b4b71cc67737765a5"),
"emp_id" : "SALS",
"emp_name" : "Pat",
"address" : {
"street" : "Court Street",
"city" : "London",
"zip" : "5698722"
}
}
From the above output it seems that, when performing equality matches on subdocuments, field order matters and the subdocuments must match exactly.
See the example below, the following query does not match the above document:
db.empinfo.find( { address: { city: "London",zip:"5698722",street: "Court Street" } } );
Previous:
MongoDB DELETE
Next:
Mongodb Shell Methods Introduction
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/mongodb/mongodb-single-field-index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics