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
// 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
// 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
// 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 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
// 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 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 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'}) |
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 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 |
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 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 |