Loading...

Follow TutorialsJar | MongoDB Tutorial on Feedspot

Continue with Google
Continue with Facebook
or

Valid

MongoDB Projection helps to return the specific fields from the query (or you can say from the MongoDB collection).  By default, when we query any collection in MongoDB, it returns all fields in matching documents.  Now, at times, we may not want all the records from the collection but a few of them in the result set. So, in that case, we use MongoDB projection (using projection document) to limit the amount/fields of data.

In previous tutorial articles, you had already seen that how we query collection documents. We use MongoDB find command to query and fetch certain records from the collection.

Syntax is –

db.collection.find();

OR

db.collection.find().pretty(); //To show the results in a better-organized way

For example, I have below collection ‘example’ with one record/document in this. Now, if I query the collection, it returns all fields from the (matching) documents by default. See snapshot below –

> db.example.find().pretty();
{
  "_id" : ObjectId("5c8392793468f8dc38ad347e"),       
  "tutorial" : "MongoDB",     
  "title" : "MongoDB Projections Tutorial",  
  "Author" : "Mohit Arora"
}

So, here you can see it has returned all the fields of the document from the collections.

But, what if we want to fetch only Tutorial or Title or Author from this document, how will we do? The answer is using MongoDB Projection. We will project specific fields to return from the query.

Before proceeding to MongoDB Projections, let’s recall how we fetch certain fields from SQL database.

Suppose, the collection ‘example’ is a table in SQL and Tutorial, Title, Author are its columns. Now, I want to fetch the only title from this table, how will I do? I will simply write below query –

select title from example; -- instead of select * from example, which will return all the columns)

On executing this query, you will get title value i.e. MongoDB Projections Tutorial.

Now, I hope you have got a clear picture of what we are trying to achieve using MongoDB Projections.

MongoDB Projection Example

Let’s see how we do projections in MongoDB. I will show you how we can fetch only title field from the collection in MongoDB using Projection (like how we saw earlier using SQL)

The syntax is –

db.collection_name.find({},{field_key:1 or 0})

** We need to set a list of fields with value 1 or 0. 1 is used to show the field while 0 is used to hide the fields.

> db.example.find({},{"title":1}).pretty();
{ 
    "_id" : ObjectId("5c8392793468f8dc38ad347e"),
    "title" : "MongoDB Projections Tutorial"
}

Now, there are two main things to consider here.

  • How we used find () method to use projection
  • _id field

Let’s understand these:

1) Find () method used projection document (as mentioned earlier) to project fields. The projection document is the second document in the find (). Hence, we have written as –

find ({},{“title”:1})

In the second document, we mentioned the field which we want to fetch. The syntactic way to do is – “fieldName”:1 //which means it will fetch Title attribute.

The question is what is this first document for. Basically, the first document is to filter the document for which we need the title attribute. Confused?

Let me add one more document in this collection. We have now following documents in the collection:

>db.example.insert({tutorial: "Python",title: "Python Tutorial", Author: "Mohit Arora"})
WriteResult({ "nInserted" : 1 })
> db.example.find().pretty();
{  
   "_id" : ObjectId("5c8392793468f8dc38ad347e"), 
   "tutorial" : "MongoDB", 
   "title" : "MongoDB Projections Tutorial", 
   "Author" : "Mohit Arora"
}
{       
    "_id" : ObjectId("5c8395a2f57d03692bcdccb6"), 
    "tutorial" : "Python",   
    "title" : "Python Tutorial", 
    "Author" : "Mohit Arora"
}
>

Now, we have two documents, one for Python and one for MongoDB. Assume, we want only the title for MongoDB tutorial.

This is how we do in SQL –

select title from example where tutorial='MongoDB';

Equivalent to this in MongoDB will be –

> db.example.find({"tutorial":"MongoDB"},{"title":1}).pretty();
{  
   "_id" : ObjectId("5c8392793468f8dc38ad347e"),
   "title" : "MongoDB Projections Tutorial"
}

So, you can see 1the st document in find () method is used to filter the records from the collection (similar to having where clause in SQL).

I had a single document (only MongoDB and not Python) while demonstrating an example earlier, so I kept empty document there. But you can have filters in the 1st document along with projection fields in the 2nd document.

2) _id Field: Note that, _MongoDB prints _id field by default. If you do not want the _id field to be fetched, you will have to specifically tell MongoDB to not to return this field. This is how we do:

We use “_id”:0 along with project fields in the projection document.

> db.example.find({"tutorial":"MongoDB"},{"title":1,"_id":0}).pretty();
{ 
   "title" : "MongoDB Projections Tutorial"
}

So, we can see MongoDB projection helps in the inclusion of certain fields to be returned in the resultset.

Now, the good thing here is you can use projections for exclusion as well. For example, in case if we want the majority of the fields from the collection except a very few. For example, let me create new MongoDB collection named ‘demo’ having some more fields.

We want to return all the fields (tutorial, title, author, type from the demo collection except Course Duration field.

To achieve this, we have two options

  • Inclusion of all fields in the query (such as “tutorial”:1, “title”:1, “author”:1, “type”:1)

Syntax:

db.demo.find({},{"tutorial":1,"title":1,"Author":1,"Type":1}).pretty();

  • Exclusion of Certain fields i.e. return all but excluded fields. (This becomes helpful when we need only very few fields to be excluded from the result set. So, instead of writing “tutorial”:1, “title”:1, “author”:1, “type”:1 we can simply write “Course Duration”:0.)

Syntax:

db.demo.find({},{"Course Duration":0}).pretty();

Note:

** As mentioned earlier, MongoDB shows _id field by default. So, we can include/exclude them based on our need.

** Projection Field is case sensitive so use wisely (See carefully how I’ve written query)

MongoDB Projection in the Embedded Document

You can use projections in the embedded documents as well. Let’s say we have the following collection-

Here, if we want to project title field, we can set the query as –

db.TutorialsJar.find({},{"details.title":1,"_id":0}).pretty();

That’s all in this MongoDB Projection tutorial. In the upcoming article, we will learn about MongoDB Operators. Do not forget to share it with your friends & colleagues. If you have any questions or feedback, do let..

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Indexes are the important aspects while reading or fetching the documents. If we do not have indexes setup, we must scan every document of the collection to find and fetch the document that matches the query statement. Doing full scan degrades the performance of the application and is very inefficient considering when we are dealing with large volume of data and processing it. But good news is that MongoDB provides very easy and efficient way to implement indexes. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect. In this MongoDB index tutorial, we will see how to create indexes, show indexes and use indexes.

Before proceeding with tutorial, let’s revise what indexes are. In brief, Indexes are special data structures. They store a small portion of the data set which can be easily traversed. It can be the value of a specific field or set of fields, ordered by the value of the field as specified while creating the index.

Note: MongoDB creates a unique index on the _id field during the creation of a collection. The _id index prevents from inserting two documents with the same value for the _id field. We cannot drop this index.

Also, MongoDB indexes use a B-tree data structure.

MongoDB Index Tutorial – Create Index (createIndex ())
  • MongoDB createIndex () method

In order to create indexes in MongoDB, we have createIndex() method. Earlier, we had ensureIndex() method to create index but it is Deprecated since version 3.0.0. db.collection.ensureIndex() is now an alias for db.collection.createIndex().

Below is the syntax:

db.collection.createIndex(Keys, Options)

where,

Keys :=  A type of document that contains the field and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for descending index, specify a value of -1

Options := (Optional) A document that contains a set of options that controls the creation of the index. Few options are:

  • background: It builds the index in the background so the operation does not block other database activities. Specify true to build in the background. The default value is false.
  • unique: It creates a unique index so that the collection will not accept insertion or update of documents where the index key value matches an existing value in the index.
  • expireAfterSeconds: Specifies a value, in seconds, to control how long MongoDB retains documents in this collection.
  • name: The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order.
  • v: (Version Number) The default index version depends on the version of MongoDB running when creating the index.

Let’s try it on Mongo shell.

Suppose we have following collection with following fields/documents

:

Let’s create index on field “blog” in ascending order using createIndex() method.

Here, you can see index is created. Another thing to note here is, there are two parameters – numIndexesBefore and numIndexesAfter.

You can see before creating any index we had 1 index created by default (i.e. for _id field), so – numIndexesBefore:1.

After creating index on ‘blog’, index count became 2, so –

numIndexesAfter:2.

MongoDB Index Tutorial – MongoDB Index Types

There are many index types provided by MongoDB to support specific types of data and queries. Below are few of them:

  • Single Field Index

MongoDB supports creating user defined indexes on single field of document in either ascending or descending order. (in addition to _id index)

//Here we are indexing student_id in descending order

          db.collection.createIndex( { student_id: -1 } )

Value of 1 specifies an index that orders items in ascending order. A value of -1 specifies an index that orders items in descending order

          *Note:  You can also create index on Embedded document/fields.

For example: Let’s assume we have following collection named as ‘tutorialsjar’:

{

  "_id": ObjectId("790c04a4ad233577f97d141"),

  "name": "TutorialsJar"

  "location": { country: "India", state: "Punjab" }

}

Now, in order to create index on state (in ascending order), we can write following:

db.tutorialsjar.createIndex( { "location.state": 1 } )

  • Compound Index

Similar to single field index, you can create compound index as well i.e. on multiple fields in a document.

E.g. We have ‘school’ collection with classes and students fields.

db.school.createIndex( { class_id: 1, student_id: -1 } )

Remember that the order of the fields listed in a compound index is important. The index will contain references to documents sorted first by the values of the class_id field (in ascending order) and, within each value of the class field, sorted by values of the student_id field (in descending order).

  • Multikey Index

If you want to index content stored in arrays, MongoDB provides Multikey Indexes. When you index a field that holds an array value, MongoDB creates separate index entries for every element of the array which allow queries to select documents that contain arrays by matching on element or elements of the arrays.

E.g. Let’s say we have following document in same collection ‘tutorialsjar’:

{ _id: 1

name: ‘Mohit’,

tags: [‘Blogging, Cricket, Music’],

color:’red’,

location:[‘India’,’US’, ‘Canada’]

}

Syntax:

db.tutorialsjar.createIndex({‘tags’:1, ‘location’:1})

** Key note here is you cannot create index on fields which are both arrays.

For example, in above example, you can create index on (tags) OR (tags, color) but NOT on (tags, location) (As both tags and location are arrays)

One interesting thing is, let’s say we have index on (tags, color). If you insert new document with tags as scalar and color as array then it is possible and it will not throw any error such as:

{

tags: ‘photography’,

color: [‘red’,’green’,’blue’]

}

  • Unique Index

The unique property for an index causes MongoDB to reject duplicate values for the indexed field. To create a unique index on a field that already has duplicate values. Other than the unique constraint, unique indexes are functionally interchangeable with other MongoDB indexes.

E.g.

db.tutorialsjar.createIndex({‘name’:1},{unique:true});

Here, ‘name’ should not have duplicate values but only unique value. If you insert same value for ‘name’ again, it will throw error.

  • Sparse Index

The sparse property of an index ensures that the index only contain entries for documents that have the indexed field. The index skips documents that do not have the indexed field. We can combine the sparse index option with the unique index option to reject documents that have duplicate values for a field but ignore documents that do not have the indexed key.

E.g.:

db.tutorialsjar.createIndex({‘name’:1},{unique:true, sparse:true});

Because in few cases, we might not have name field (only personal ID is there). Hence, it will have NULL which will violate unique constraint. So, using Sparse index with Unique index is efficient.

  •  Geospatial Index

MongoDB supports Geospatial index for querying geospatial coordinate data. There are two indexes:

  1. 2d indexes – Uses planar geometry when returning results (Use a 2dindex for data stored as points on a two-dimensional plane).

Suppose we have location field with some X & Y coordinates such as:

{“Location”: [X,Y]}

We can create index like:

db.tutorialsjar.createIndex({“location”:”2d”});

In order to find documents:

db.tutorialsjar.find({“location”:{$near:[50,50]}});

If you are not much confident with Find command, youc an read my other article on MongoDB Find Command.

2)  2d sphere indexes – Uses spherical geometry to return results. (Calculate geometries on an earth-like sphere. 2dsphereindex supports all MongoDB geospatial queries: queries for inclusion, intersection and proximity.)

E.g. (taken from MongoDB site)

db.places.insert(

   {

      loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },

      name: "Central Park",

      category : "Parks"

   }

)

db.places.insert(

   {

      loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },

      name: "La Guardia Airport",

      category : "Airport"

   }

)

Syntax to create index:

db.places.createIndex( { loc : "2dsphere" } );

**You can also create compound index with non-geospatial index keys.

db.places.createIndex( { loc : "2dsphere" , category : 1} )

A compound 2dsphere index does not require the location field to be the first field indexed. You can also write as:

db.places.createIndex( { category : 1 , loc : "2dsphere" } ) ;

Now, in order to find documents using 2dsphere indexes:

db.tutorialsjar.find({“location”: {$near:{$geometry:{type:”point”, coordinates:[-122.16,47.78]},$maxDistance: 2000)}}}).pretty();

  • Text Index

 MongoDB provides text indexes to support text search queries on string content. text indexes can include any field whose value is a string or an array of string elements.

Note that a collection can have at most one text index.

E.g. Suppose we have ‘Animal’ collection with following documents:

{_id:1, “words”: ”dogs and cats”, “seq”:1}

{_id:2, “words”: ”dogs and cows”,”seq”:2 }

In order to create index:

db.animal.createIndex({“words”:’text’});

To find documents:

db.animal.find({$text:{$search:”dogs”}});

** Capitalization also does not make any difference. It will search same for ‘Dog’

  • TTL Index (Total Time To Live Index)

TTL indexes are special type of indexes which can be used to automatically remove documents from a collection after a certain amount of time. Few documents we might not need after some period of time such as logs data, session information, audit data, machine generated logs etc. So, in order to remove these type of documents, we can use MongoDB TTL indexes.

Syntax:

db.auditlog.createIndex( { "lastAuditDate": 1 }, { expireAfterSeconds: 3600 } );

MongoDB index Tutorial – List All Indexes or Show Indexes or Get Indexes

MongoDB provides getIndexes() method to return a list of all indexes on a collection.

Syntax:

db.collection.getIndexes()

For example, to view all indexes on the tutorialsjar collection, run the following command:

db.tutorialsjar.getIndexes();

You can see there are 2 indexes created on this collection but we had created only 1 index on ‘blog’. However, you can see there is another index created on _id by default.

If there is no index created on collection, by default it will return 1 index (i.e. for _id field which is by default indexed)

MongoDB Drop Indexes or Remove Indexes

MongoDB provides two methods for removing indexes from a collection:

  • dropIndex()

To remove a specific index, use db.collection.dropIndex().

For example, the following operation removes an index on the name field in the tutorialsjar colelction:

db.tutorialsjar.dropIndex( { "name": 1 } );

  • dropIndexes()

To remove all indexes from collection, use db.collection.dropIndexes().

For example, the following command removes all indexes from the ‘tutorialsjar’ collection:

db.tutorialsjar.dropIndexes();

“nIndexesWas”: 2 shows total indexes in collecton (including _id field index) before dropping indexes.

Note that it does not drop index created on _id field. You can see message – “msg”: “non-_id indexes dropped for collection.”

That is all in this MongoDB Index Tutorial article. I hope you found something new to learn here. Do not forget to share it with your friends.

Is there anything else we should have included in this MongoDB indexing article? Do let us know in the comment section below.

The post MongoDB Index Tutorial – Create Index, Show Indexes & Index Types appeared first on TutorialsJar.

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Separate tags by commas
To access this feature, please upgrade your account.
Start your free month
Free Preview