@@ -1011,7 +1011,7 @@ Db.prototype.createCollection = function(collectionName, options, callback) {
10111011
10121012 // Create a new collection and return it
10131013 self . _executeQueryCommand ( DbCommand . createCreateCollectionCommand ( self , collectionName , options )
1014- , { read :false , safe :safe }
1014+ , { read :'primary' , safe :safe }
10151015 , utils . handleSingleCommandResultReturn ( null , null , function ( err , result ) {
10161016 if ( err ) return callback ( err , null ) ;
10171017 // Create collection and return
@@ -1175,7 +1175,7 @@ Db.prototype.executeDbAdminCommand = function(command_hash, options, callback) {
11751175 }
11761176
11771177 if ( options . readPreference ) {
1178- options . read = options . readPreference ;
1178+ options . readPreference = options . readPreference ;
11791179 }
11801180
11811181 this . _executeQueryCommand ( DbCommand . createAdminDbCommand ( this , command_hash ) , options , function ( err , result ) {
@@ -1592,23 +1592,22 @@ var bindToCurrentDomain = function(callback) {
15921592 */
15931593var __executeQueryCommand = function ( self , db_command , options , callback ) {
15941594 // Options unpacking
1595- var read = options [ 'read' ] != null ? options [ 'read' ] : false ;
1596- read = options [ 'readPreference' ] != null && options [ 'read' ] == null ? options [ 'readPreference' ] : read ;
1595+ var readPreference = options . readPreference != null ? options . readPreference : 'primary' ;
15971596 var raw = options [ 'raw' ] != null ? options [ 'raw' ] : self . raw ;
15981597 var onAll = options [ 'onAll' ] != null ? options [ 'onAll' ] : false ;
15991598 var specifiedConnection = options [ 'connection' ] != null ? options [ 'connection' ] : null ;
16001599
1601- // Correct read preference to default primary if set to false, null or primary
1602- if ( ! ( typeof read == 'object' ) && read . _type == 'ReadPreference' ) {
1603- read = ( read == null || read == 'primary' || read == false ) ? ReadPreference . PRIMARY : read ;
1604- if ( ! ReadPreference . isValid ( read ) ) return callback ( new Error ( "Illegal readPreference mode specified, " + read ) ) ;
1605- } else if ( typeof read == 'object' && read . _type == 'ReadPreference' ) {
1606- if ( ! read . isValid ( ) ) return callback ( new Error ( "Illegal readPreference mode specified, " + read . mode ) ) ;
1600+ // Correct readPreference preference to default primary if set to false, null or primary
1601+ if ( ! ( typeof readPreference == 'object' ) && readPreference . _type == 'ReadPreference' ) {
1602+ readPreference = ( readPreference == null || readPreference == 'primary' || readPreference == false ) ? ReadPreference . PRIMARY : readPreference ;
1603+ if ( ! ReadPreference . isValid ( readPreference ) ) return callback ( new Error ( "Illegal readPreference mode specified, " + readPreference ) ) ;
1604+ } else if ( typeof readPreference == 'object' && readPreference . _type == 'ReadPreference' ) {
1605+ if ( ! readPreference . isValid ( ) ) return callback ( new Error ( "Illegal readPreference mode specified, " + readPreference . mode ) ) ;
16071606 }
16081607
16091608 // If we have a read preference set and we are a mongos pass the read preference on to the mongos instance,
1610- if ( self . serverConfig . isMongos ( ) && read != null && read != false ) {
1611- db_command . setMongosReadPreference ( read ) ;
1609+ if ( self . serverConfig . isMongos ( ) && readPreference != null && readPreference != 'primary' ) {
1610+ db_command . setMongosReadPreference ( readPreference ) ;
16121611 }
16131612
16141613 // If we got a callback object
@@ -1619,10 +1618,10 @@ var __executeQueryCommand = function(self, db_command, options, callback) {
16191618
16201619 if ( connection instanceof Error ) return callback ( connection , null ) ;
16211620
1622- // Fetch either a reader or writer dependent on the specified read option if no connection
1621+ // Fetch either a reader or writer dependent on the specified readPreference option if no connection
16231622 // was passed in
16241623 if ( connection == null ) {
1625- connection = self . serverConfig . checkoutReader ( read ) ;
1624+ connection = self . serverConfig . checkoutReader ( readPreference ) ;
16261625 }
16271626
16281627 if ( connection == null ) {
@@ -1687,8 +1686,7 @@ var __executeQueryCommand = function(self, db_command, options, callback) {
16871686 }
16881687 } else {
16891688 // Fetch either a reader or writer dependent on the specified read option
1690- // var connection = read == null || read == 'primary' || read == false ? self.serverConfig.checkoutWriter(true) : self.serverConfig.checkoutReader(read);
1691- var connection = self . serverConfig . checkoutReader ( read ) ;
1689+ var connection = self . serverConfig . checkoutReader ( readPreference ) ;
16921690 // Override connection if needed
16931691 connection = specifiedConnection != null ? specifiedConnection : connection ;
16941692 // Ensure we have a valid connection
@@ -1743,17 +1741,17 @@ Db.prototype._executeQueryCommand = function(db_command, options, callback) {
17431741
17441742 // Get the configuration
17451743 var config = this . serverConfig ;
1746- var read = options . read ;
1744+ var readPreference = options . readPreference ;
17471745 // Allow for the usage of the readPreference model
1748- if ( read == null ) {
1749- read = options . readPreference ;
1746+ if ( readPreference == null ) {
1747+ readPreference = options . readPreference ;
17501748 }
17511749
1752- if ( ! connection && ! config . canRead ( read ) && ! config . canWrite ( ) && config . isAutoReconnect ( ) ) {
1753- if ( read == ReadPreference . PRIMARY
1754- || read == ReadPreference . PRIMARY_PREFERRED
1755- || ( read != null && typeof read == 'object' && read . mode )
1756- || read == null ) {
1750+ if ( ! connection && ! config . canRead ( readPreference ) && ! config . canWrite ( ) && config . isAutoReconnect ( ) ) {
1751+ if ( readPreference == ReadPreference . PRIMARY
1752+ || readPreference == ReadPreference . PRIMARY_PREFERRED
1753+ || ( readPreference != null && typeof readPreference == 'object' && readPreference . mode )
1754+ || readPreference == null ) {
17571755
17581756 // Save the command
17591757 self . serverConfig . _commandsStore . read_from_writer (
@@ -1783,7 +1781,7 @@ Db.prototype._executeQueryCommand = function(db_command, options, callback) {
17831781 if ( ! self . serverConfig . _commandsStore . validateBufferLimit ( self . bufferMaxEntries ) ) {
17841782 self . close ( ) ;
17851783 }
1786- } else if ( ! connection && ! config . canRead ( read ) && ! config . canWrite ( ) && ! config . isAutoReconnect ( ) ) {
1784+ } else if ( ! connection && ! config . canRead ( readPreference ) && ! config . canWrite ( ) && ! config . isAutoReconnect ( ) ) {
17871785 return callback ( new Error ( "no open connections" ) , null ) ;
17881786 } else {
17891787 if ( typeof callback == 'function' ) {
0 commit comments