10/7/2019 - 9:31 AM

Cheat codes - DDL

//creates DB if not created
use mydb

//view current db

//show db list
show dbs OR show databases

//creates collection if not created
db.movie.insert({"name":"tutorials point"})

//Show help for database methods.

// Show help on collection methods. The <collection> can be the name of an existing collection or a non-existing collection.

//Print a list of all collections for current database.
show collections

show users
show roles
show profile

//drop db

//create db
db.createCollection(name, options)
Name	-Name of the collection to be created
Options	(Optional) - Specify options about memory size and indexing
capped -	Boolean	(Optional) - If true, enables a capped collection. Capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also.
autoIndexId -	Boolean	(Optional) - If true, automatically create index on _id field.s Default value is false.
size	number -	(Optional) - Specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also.
max	number -	(Optional) - Specifies the maximum number of documents allowed in the capped collection.

//drop collection

mongodump //dump db
mongorestore //restore db
mongostat //This command checks the status of all running mongod instances and return counters of database operations.
mongotop //This command tracks and reports the read and write activity of MongoDB instance on a collection basis.

// Analyzing queries is a very important aspect of measuring how effective the database and indexing design is. We will learn about the frequently used $explain and $hint queries.
//The $hint operator forces the query optimizer to use the specified index to run a query. This is particularly useful when you want to test performance of a query with different indexes. For example, the following query specifies the index on fields gender and user_name to be used for this query −
//To analyze the above query using $explain −
//Indexes support the efficient resolution of queries. 
//To create an index you need to use ensureIndex() method of MongoDB.
//Here key is the name of the field on which you want to create index and 1 is for ascending order. 
//To create index in descending order you need to use -1.
//ensureIndex() method also accepts list of options (which are optional). Following is the list −
background -	Boolean -	Builds the index in the background so that building an index does not block other database activities. Specify true to build in the background. The default value is false.
unique -	Boolean -	Creates a unique index so that the collection will not accept insertion of documents where the index key or keys match an existing value in the index. Specify true to create a unique index. The default value is false.
name -	string -	The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order.
dropDups -	Boolean -	Creates a unique index on a field that may have duplicates. MongoDB indexes only the first occurrence of a key and removes all documents from the collection that contain subsequent occurrences of that key. Specify true to create unique index. The default value is false.
sparse -	Boolean -	If true, the index only references documents with the specified field. These indexes use less space but behave differently in some situations (particularly sorts). The default value is false.
expireAfterSeconds -	integer -	Specifies a value, in seconds, as a TTL to control how long MongoDB retains documents in this collection.
v -	index - version	The index version number. The default index version depends on the version of MongoDB running when creating the index.
weights -	document -	The weight is a number ranging from 1 to 99,999 and denotes the significance of the field relative to the other indexed fields in terms of the score.
default_language -	string -	For a text index, the language that determines the list of stop words and the rules for the stemmer and tokenizer. The default value is english.
language_override -	string -	For a text index, specify the name of the field in the document that contains, the language to override the default language. The default value is language.

//Covered Queries
// As per the official MongoDB documentation, a covered query is a query in which −
// - All the fields in the query are part of an index.
// - All the fields returned in the query are in the same index.
// Since all the fields present in the query are part of an index, MongoDB matches the query conditions and returns the result using the same index without actually looking inside the documents. Since indexes are present in RAM, fetching data from indexes is much faster as compared to fetching data by scanning documents.

   "_id": ObjectId("53402597d852426020000002"),
   "contact": "987654321",
   "dob": "01-01-1991",
   "gender": "M",
   "name": "Tom Benzamin",
   "user_name": "tombenzamin"
db.users.find({gender:"M"},{user_name:1,_id:0}) //Covered Query
//Since our index does not include _id field, we have explicitly excluded it from result set
// Starting from version 2.4, MongoDB started supporting text indexes to search inside string content. The Text Search uses stemming techniques to look for specified words in the string fields by dropping stemming stop words like a, an, the, etc. At present, MongoDB supports around 15 languages.
//Enabling Text Search

//Creating Text Index
   "post_text": "enjoy the mongodb articles on tutorialspoint",
   "tags": [

//Using Text Index