Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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_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 / geospatial_index.js
Last active August 29, 2015 14:03
Geospatial Index In Mongodb
// Let's say , we have a geo database that has places collection in it
use geo
db.places.insert({'name': 'pepito', 'type': 'dept. store', 'location': [40, 70]})
db.places.insert({'name': 'circle-k', 'type': 'mini mart', 'location': [40.232, -74.343]})
db.places.insert({'name': 'ayunadi', 'type': 'grocery', 'location': [41.232, -75.343]})
// Next, we want to put an index on the location field.
// Since the location's value is the x-y coordinate of a place, we'll put '2d' index on it.
var index = {'location': '2d', 'type':1}
db.places.ensureIndex(index)
@saintc0d3r
saintc0d3r / geospatial_spherical_index.js
Last active August 29, 2015 14:03
Geospatial Spherical Index in MongoDb
// Let's say , we have a geo database that has places collection in it
use geo
db.places.insert({'name': 'pepito', 'city': 'Badung, Bali', 'type': 'retail', 'location': {'type': 'Point', 'coordinates': [-8.738151,-25.1735585]}})
db.places.insert({'name': 'circle-k', 'city': 'Tuban, Bali', 'type': 'mini mart', 'location': {'type': 'Point', 'coordinates': [-8.7358627,-25.168867000000006]}})
db.places.insert({'name': 'ayunadi', 'city': 'Tuban, Bali', 'type': 'grocery', 'location': {'type': 'Point', 'coordinates': [-8.7394041,-25.1675752]}})
// Next, we'll put '2dsphere' index on the location field
// Caution: 2dsphere is limited to the range -180,180 for x coordinate/longitude, and -90,90 for y coordinate/latitude !
var index = {'location':'2dsphere', 'type':1, 'city':1}
db.places.ensureIndex(index)
@saintc0d3r
saintc0d3r / text_search_index.js
Created June 29, 2014 14:29
Text Search Index in MongoDB
// Let's say we have a documents db where it has a sentences collection
use documents
db.sentences.insert({'words': 'Cat moss granite.'})
db.sentences.insert({'words': 'dog tree ruby.'})
db.sentences.insert({'words': 'dog tree obsidian.'})
db.sentences.insert({'words': 'dog tree granite.'})
db.sentences.insert({'words': 'dog shrub ruby.'})
db.sentences.insert({'words': 'dog shrub obsidian.'})
db.sentences.insert({'words': 'dog shrub granite.'})
db.sentences.insert({'words': 'dog moss ruby.'})