Skip to content

Instantly share code, notes, and snippets.

@saintc0d3r
saintc0d3r / hint_performance.js
Created June 29, 2014 10:52
Using hint to increase specific query's performance on indexed collections
// Suppose, we have 10000000 documents stored in bar collections (foo database)
use foo
for(var i = 0; i< 10000000; i++)(db.bar.insert({'a':i, 'b':i, 'c':i}))
// We create indexes for them
db.bar.ensureIndex({'a':1})
db.bar.ensureIndex({'b':1})
db.bar.ensureIndex({'c':1})
db.bar.ensureIndex({'a':1, 'b':1,'c':1})
@saintc0d3r
saintc0d3r / explain_hint_in_node.js
Created June 29, 2014 09:56
Calling explain & hint in node.js
// Hint sample
var MongoClient = require('mongodb').MongoClient;
function query_hint_helper(db, query, hint, close_db){
var cursor = db.collection('bar').find(query, {}, hint);
// Explain the query
cursor.explain(function(err, explain_output){
if (err) throw err;
@saintc0d3r
saintc0d3r / hint.js
Created June 29, 2014 07:54
Telling mongodb to use specific index when doing a query
// Let's say there is foobar db which have foo collection with some documents in it
use foo
var i = 0
for(i=0;i < 100;i++)(db.bar.insert({'a':i, 'b':i, 'c':i, 'd':i}))
// and we create indexes for each field
db.bar.ensureIndex({'a':1})
db.bar.ensureIndex({'b':1})
db.bar.ensureIndex({'c':1})
db.bar.ensureIndex({'d':1})
@saintc0d3r
saintc0d3r / stats_collection.js
Created June 28, 2014 12:21
Showing the stats of a mongodb's collection
// To show the statistic of a foo collection
db.foo.stats()
// To show the size of total index size of foo collection (in bytes)
db.foo.totalIndexSize()
@saintc0d3r
saintc0d3r / understanding_explain.js
Created June 28, 2014 11:59
Understanding Explain in related to mongodb's index
// Let's say that there is a 'foo' collection has 10 mill documents in it
use foobar
for(var i=0;i<10000000;i++){db.foo.insert({'a':i,'b':i,'c':i})}
// Then, we'll add indexes for a,b,c on the 'foo' collection
db.foo.ensureIndex({'a':1, 'b':1, 'c':1})
// Let's do querying a doco by a field as the search criteria then note the millis value in the displayed result
db.foo.find({'a': 5678545}).explain()
@saintc0d3r
saintc0d3r / sparse_index.js
Last active August 29, 2015 14:03
Sparse index on mongodb
// Let's create a tournaments database where it has fighters table with few documents in it
use tournaments
db.fighters.insert({'name':'Ryu', 'ultimate': 'shinkuu hadou ken'})
db.fighters.insert({'name':'Ken', 'ultimate': 'Shoryureppa'})
db.fighters.insert({'name':'Dan'})
db.fighters.insert({'name':'Zangief'})
// We want to put an index on the name & ultimate fields to speed its query by name's performance (no multi key index applied)
db.fighters.ensureIndex({'name':1, 'ultimate':1})
@saintc0d3r
saintc0d3r / add_unique_remove_duplicates.js
Created June 27, 2014 18:00
Add unique index while also remove the documents which has duplicated key index
// Let's say there is a fruits collection where it contains documents as follow
db.fruits.insert({'name':'pear'})
db.fruits.insert({'name':'apple'})
db.fruits.insert({'name':'pear'})
// Then, we want to prevent another document with name = pear can be inserted into fruits collection
// In the other words, we want to make the name property become unique.
// And also, duplicated document would be removed
db.fruits.ensureIndex({'name':1}, {'unique':true, 'dropDups':true})
@saintc0d3r
saintc0d3r / unique_index.js
Created June 27, 2014 14:33
Unique Index on a mongodb's collection
// Let's say there is a fruits collection where it contains a pear document
db.fruits.insert({'name':'pear'})
// Then, we want to prevent another document with name = pear can be inserted into fruits collection
// In the other words, we want to make the name property become unique
db.fruits.ensureIndex({'name':1}, {'unique':true})
// Let's push a new document that has different name than to the existing one
db.fruits.insert({'name':'apel'})
@saintc0d3r
saintc0d3r / multi_key_index.js
Last active August 29, 2015 14:03
Muilti key Indexes on mongodb
// Let's say we have a database named as 'foo'
use foo
// On the foo database, there is a collection named as 'bar' & we'll add a record on 'bar' collection
db.bar.insert({'a':1, 'b':5})
// Now, we are going to add indexes for both a & b
db.bar.ensureIndex({'a':1, 'b': -1})
// Let's see which index the bar uses when querying it a or/and b. Notice what is the values of "cursor" & "isMultiKey" in the returned result
@saintc0d3r
saintc0d3r / view_drop_indexes.js
Created June 26, 2014 13:41
View & Dropping indexes in mongodb
// Let's say, there is a database in the mongodb server named as 'school'. Let's use this database.
use school
// Let's view a list of available indexes on the 'school' database.
db.system.indexes.find()
// Let the 'school' db has a collection named as 'students'. Then, we want to list indexes on a particular collection ('students')
db.students.getIndexes()
// Supposed, there is an index for one of 'students' columns, let's say '_id'. Then, we want to delete this index