This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.'}) |