MongoDB

Remove and Modify a Specific Index

To remove an index, use the db.collection.dropIndex() method.

 db.users.dropIndex( { "userid": 1 } ) //removes index from userid field  

db.users.dropindexes() //removes all indexes from users collection except _id index


To modify the index, you must drop the index first and then recreate the index.

 db.users.dropIndex( { "userid": 1 } ) //removes index from userid field  

db.users.createIndex( { "userid": 1 }, { unique: true } ) //Unique Index on a Single Field
MongoDB

Create an Index in MongoDB

Indexes allow to process and fulfill queries quickly by creating small and efficient representations of the documents in a collection. By default, MongoDB creates an index on the _id field of every collection.

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

 db.users.createIndex( { userid: 1 } ) //Create an Index on a Single Field   

db.users.createIndex( { userid: 1, Email: 1 } ) //Build a Compound Index

db.users.createIndex( { Email: 1 }, { unique: true } ) //Unique Index on a Single Field

db.users.createIndex( { userid: 1, Email: 1 }, { unique: true } ) //Unique Index on a Compund Field

//Hashed indexes compute a hash of the value of a field in a collection and index the hashed value
db.users.createIndex( { _id: "hashed" } ) //Create a Hashed Index

//Background index construction allows read and write operations to continue while building the index.
db.users.createIndex( { Email: 1 }, { background: true } ) //Build Indexes in the Background

//The following operation, creates a sparse index on the users collection that only includes a document in the index if the Email field exists in a document.
//The index excludes all documents that do not include the Email field.
db.users.createIndex( { Email: 1 }, { sparse: true } ) //Create a Sparse Index
MongoDB

Create an Auto-Incrementing Sequence Field

Example:-
Use a dotnetfundacounter collection to track the last number sequence used. The _id field contains the sequence name and the seq field contains the last value of the sequence.

Insert into the dotnetfundacounter collection, the initial value for the userid:

 db.dotnetfundacounter.insert(  
{
_id: "userid",
seq: 0
}
)


Create a getNextSequenceBydoc function that accepts a docname of the sequence. The function uses the findAndModify() method to atomically increment the seq value and return this new value

 function getNextSequenceBydoc(docname) {   
var ret = db.dotnetfundacounter.findAndModify(
{
query: { _id: docname },
update: { $inc: { seq: 1 } },
new: true,
upsert: true
}
);
return ret.seq;
}

Use this getNextSequenceBydoc() function during insert()

 db.users.insert(  
{
_id: getNextSequenceBydoc("userid"),
Name: "Himen Patel"
}
)
MongoDB

Include or Exclude Fields to Return from a Query

The document can specify the inclusion of fields or the exclusion of fields. The specifications have the following forms.

  • Specify to include a field – 1 or TRUE
  • Specify to suppression/Exclude a field – 0 or FALSE
     db.users.find( { Name: 'Himen Patel' }, { Name: 1, Email: 1 } )  

    In the result set, only the Name and Email fields and, by default, the _id field returns in the matching document (users). To remove the _id field from the results specifying it in exclusion.

     db.users.find( { Name: 'Himen Patel' }, { Name: 1, Email: 1, _id:0 } )