Created
June 29, 2014 09:56
-
-
Save saintc0d3r/174018c34ffbe68aa9f0 to your computer and use it in GitHub Desktop.
Calling explain & hint in node.js
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; | |
console.log('Hint output: '); | |
console.dir(explain_output); | |
if (close_db) return db.close; | |
}); | |
} | |
MongoClient.connect('mongodb://localhost:27017/foo', function(err, db){ | |
if (err) throw err; | |
var query = {'a':50, 'b':50, 'c':50}; | |
// Make a query but returns cursor. Also, hint the query to NOT using any indexes | |
query_hint_helper(db, query, {'hint': {'$natural': 1}}); | |
// Let's spawn another cursor to do same query but with the hint that point to an existing index | |
query_hint_helper(db, query, {'hint': {'a': 1}}, 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
// Explain sample | |
var MongoClient = require('mongodb').MongoClient; | |
MongoClient.connect('mongodb://localhost:27017/foo', function(err, db){ | |
if (err) throw err; | |
// Make a query but returns cursor | |
var cursor = db.collection('bar').find({'a':50, 'b':50, 'c':50}); | |
// Explain the query | |
cursor.explain(function(err, explain_output){ | |
if (err) throw err; | |
console.dir(explain_output); | |
return db.close(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment