MongoDB连接

Edit

学习笔记 mongo

名词解释

shard

官方文档Sharding Introduction

Sharding is a method for storing data across multiple machines.

中文称为分片。参考8天学通MongoDB——第六天 分片技术

sharded cluster

片簇

replica set

A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments.

每一个replica set可以有自己的tag来控制read preference和write concern,参考官方文档Configure Replica Set Tag Sets

write concern

官方文档Write Concern

Write concern describes the level of acknowledgement requested from MongoDB for write operations to a standalone mongod or to replica sets or to sharded clusters. In sharded clusters, mongos instances will pass the write concern on to the shards.

Write concern can include the following fields:

{ w: \<value\>, j: \<boolean\>, wtimeout: \<number\> }

  • w: 1 - 有acknowledgement,默认值; 0 - 没有acknowledgement,数据库可能把错误信息传给客户端
  • w: ‘majority’ & ‘\\’
    • 前者:Requests acknowledgment that write operations have propagated to the majority of voting nodes [1], including the primary, and have been written to the on-disk journal for these nodes. 此选项包含了j: true。
    • 后者:Requests acknowledgement that the write operations have propagated to a replica set member with the specified tag.
  • j: true & false, 决定是否写journal
  • wtimeout: This option specifies a time limit, in milliseconds, for the write concern. wtimeout is only applicable for w values greater than 1.

If you specify w: 0 but include j: true, the j: true prevails to request acknowledgement from the standalone mongod or the primary of a replica set.

journal

A sequential, binary transaction log used to bring the database into a valid state in the event of a hard shutdown. Journaling writes data first to the journal and then to the core data files. MongoDB enables journaling by default for 64-bit builds of MongoDB version 2.0 and newer. Journal files are pre-allocated and exist as files in the data directory. See Journaling.

mongos

mongos for “MongoDB Shard,” is a routing service for MongoDB shard configurations that processes queries from the application layer, and determines the location of this data in the sharded cluster, in order to complete these operations. From the perspective of the application, a mongos instance behaves identically to any other MongoDB instance.

mongos vs. mongod

mongodb采用将集合进行拆分,然后将拆分的数据均摊到几个片上的一种解决方案。

连接mongo数据库

我收集了好几篇讲如何更好的连接mongo数据库的文章。参考下面的笔记:
mongodb驱动的正确使用方法
mongodb 连接的问题
node-mongo-native1.3.19连接mongo的最优方法
还有node.js的MongoDB driverd的官方介绍:
MongoClient or how to connect in a new and better way
还有人用连接池的
生产级Nodejs开发实践-使用连接池

Old Style

先看一段代码

1.var server_options={};
2.var db_options={w:-1};
3.
4.var mongodb = require("mongodb"),
5. mongoserver = new mongodb.Server('localhost', 27017,server_options ),
6. db = new mongodb.Db('test', mongoserver, db_options);
7.
8.var http=require('http');
9.
10.var server=http.createServer(function(req,res){
11. db.open(function(err,db){
12. if(err)return console.error(err);
13. console.log('* mongodb connected');
14. db.collection('foo').save({test:1},function(err,result){
15. res.end(JSON.stringify(result,null,2));
16. db.close();
17. });
18. })
19.
20.});

每来一个http请求,就开关一次数据库。更搞的是,new mongodb.Server的参数server_options里可以有一个poolSize,如果不指定的话,默认值就是5,也就是说每开一次数据库,实际上是开了5个数据库连接。这也就是为什么我们经常在数据库运行终端中经常至少看见5个connections的原因。这也是为什么我们connections疯涨的原因。
另外在old style操作代码中,还有一点值得注意的是,close是不会close读连接的,它只会close写连接,要强制close所有连接要用close(true)。

close
Close the current db connection, including all the child db instances. Emits close event and calls optional callback.

close([forceClose], callback)
Arguments:

  • [forceClose] (boolean) – connection can never be reused.
  • callback (function) – this will be called after executing this method. The first parameter will contain the Error object if an error occurred, or null otherwise. While the second parameter will contain the results or null if an error occurred.

Returns: null

New Style

参考官方代码:

1.var express = require('express'); 
2.var mongodb = require('mongodb'); var app = express();
3.
4.var MongoClient = require('mongodb').MongoClient;
5.var db;
6.
7.// Initialize connection once MongoClient.connect("mongodb://localhost:27017/integration_test", {
8. db: {
9. native_parser: false
10. },
11. server: {
12. socketOptions: {
13. connectTimeoutMS: 500
14. }
15. },
16. replSet: {},
17. mongos: {}
18.},function(err, database) {
19. if(err) throw err;
20. db = database;
21.
22. // Start the application after the database connection is ready
23. app.listen(3000);
24. console.log("Listening on port 3000");
25.});
26.
27.// Reuse database object in request handlers
28.app.get("/", function(req, res) {
29. db.collection("replicaset_mongo_client_collection").find({},function(err, docs) {
30. docs.each(function(err, doc) {
31. if(doc) {
32. console.log(doc);
33. }
34. else {
35. res.end();
36. }
37. });
38. });
39.});

MongoClient.connect options

主要有来自4个方面的设置:

  • db
  • server
  • replica set
  • mongos
    后两个针对sharding,这次我们主要关注前两个。从官方文档里挑出几条配置罗列一下:

db: A hash of options at the db level overriding or adjusting functionality not supported by the url

  • w, {Number/String, > -1 || ‘majority’} the write concern for the operation where < 1 is no acknowledgment of write and w >= 1 or w = ‘majority’ acknowledges the write
  • wtimeout, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
  • fsync, (Boolean, default:false) write waits for fsync before returning
  • journal, (Boolean, default:false) write waits for journal sync before returning
  • readPreference {String}, the preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  • native_parser {Boolean, default:false}, use c++ bson parser.
  • forceServerObjectId {Boolean, default:false}, force server to create _id fields instead of client.
  • pkFactory {Object}, object overriding the basic ObjectID primary key generation.
  • serializeFunctions {Boolean, default:false}, serialize functions.
  • raw {Boolean, default:false}, perform operations using raw bson buffers.
  • recordQueryStats {Boolean, default:false}, record query statistics during execution.
  • retryMiliSeconds {Number, default:5000}, number of milliseconds between retries.
  • numberOfRetries {Number, default:5}, number of retries off connection.
  • bufferMaxEntries {Number, default: -1}, sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited.

server: A hash of options at the server level not supported by the url.

  • readPreference {String, default:null}, set’s the read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST)
  • ssl {Boolean, default:false}, use ssl connection (needs to have a mongod server with ssl support)
  • slaveOk {Boolean, default:false}, legacy option allowing reads from secondary, use readPrefrence instead.
  • poolSize {Number, default:1}, number of connections in the connection pool, set to 1 as default for legacy reasons.
  • socketOptions {Object, default:null}, an object containing socket options to use (noDelay:(boolean), keepAlive:(number), connectTimeoutMS:(number), socketTimeoutMS:(number))
  • logger {Object, default:null}, an object representing a logger that you want to use, needs to support functions debug, log, error ({error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}}).
  • auto_reconnect {Boolean, default:false}, reconnect on error.
  • disableDriverBSONSizeCheck {Boolean, default:false}, force the server to error if the BSON message is to big

我们关注的选项,我用粗体做了标注。

另外我们在前文中提到,有些文章推荐用generic-pool为mongo连接提供连接池。但是,我发现MongoClient.connect的option里就有poolSize。在实际项目中直接将之赋为100,也可以得到不错的效果。

%23MongoDB%u8FDE%u63A5%0A@%28%u5B66%u4E60%u7B14%u8BB0%29%5Bmongo%5D%0A%23%23%u540D%u8BCD%u89E3%u91CA%0A%23%23%23%20shard%0A%u5B98%u65B9%u6587%u6863%5BSharding%20Introduction%5D%28https%3A//docs.mongodb.org/manual/core/sharding-introduction/%29%uFF1A%0A%3E%20Sharding%20is%20a%20method%20for%20storing%20data%20across%20multiple%20machines.%0A%0A%u4E2D%u6587%u79F0%u4E3A%u5206%u7247%u3002%u53C2%u8003%5B8%u5929%u5B66%u901AMongoDB%u2014%u2014%u7B2C%u516D%u5929%20%u5206%u7247%u6280%u672F%5D%28https%3A//www.evernote.com/shard/s24/nl/697376823/c9ca296a-ae2b-4833-bcb5-a37213b4cafc/%29%u3002%0A%0A%23%23%23%20sharded%20cluster%0A%u7247%u7C07%0A%0A%23%23%23%20replica%20set%0A%3EA%20replica%20set%20in%20MongoDB%20is%20a%20group%20of%20mongod%20processes%20that%20maintain%20the%20same%20data%20set.%20Replica%20sets%20provide%20redundancy%20and%20high%20availability%2C%20and%20are%20the%20basis%20for%20all%20production%20deployments.%20%0A%0A%u6BCF%u4E00%u4E2Areplica%20set%u53EF%u4EE5%u6709%u81EA%u5DF1%u7684tag%u6765%u63A7%u5236read%20preference%u548Cwrite%20concern%uFF0C%u53C2%u8003%u5B98%u65B9%u6587%u6863%5BConfigure%20Replica%20Set%20Tag%20Sets%5D%28https%3A//docs.mongodb.org/manual/tutorial/configure-replica-set-tag-sets/%23replica-set-configuration-tag-sets%29%u3002%0A%0A%23%23%23%20write%20concern%0A%u5B98%u65B9%u6587%u6863%5BWrite%20Concern%5D%28https%3A//docs.mongodb.org/manual/reference/write-concern/%29%0A%3EWrite%20concern%20describes%20the%20level%20of%20acknowledgement%20requested%20from%20MongoDB%20for%20write%20operations%20to%20a%20standalone%20mongod%20or%20to%20replica%20sets%20or%20to%20sharded%20clusters.%20In%20sharded%20clusters%2C%20mongos%20instances%20will%20pass%20the%20write%20concern%20on%20to%20the%20shards.%0A%0AWrite%20concern%20can%20include%20the%20following%20fields%3A%0A%3E%7B%20w%3A%20%5C%3Cvalue%5C%3E%2C%20j%3A%20%5C%3Cboolean%5C%3E%2C%20wtimeout%3A%20%5C%3Cnumber%5C%3E%20%7D%0A%3E%20-%20w%3A%201%20-%20%u6709acknowledgement%uFF0C%u9ED8%u8BA4%u503C%3B%200%20-%20%u6CA1%u6709acknowledgement%uFF0C%u6570%u636E%u5E93%u53EF%u80FD%u628A%u9519%u8BEF%u4FE1%u606F%u4F20%u7ED9%u5BA2%u6237%u7AEF%0A%3E%20-%20w%3A%20%u2018majority%u2019%20%26%20%27%5C%3Ctag%20set%3E%5C%27%0A%09*%20%u524D%u8005%uFF1ARequests%20acknowledgment%20that%20write%20operations%20have%20propagated%20to%20the%20majority%20of%20voting%20nodes%20%5B1%5D%2C%20including%20the%20primary%2C%20**and%20have%20been%20written%20to%20the%20on-disk%20journal%20for%20these%20nodes**.%20%u6B64%u9009%u9879%u5305%u542B%u4E86j%3A%20true%u3002%0A%09*%20%u540E%u8005%uFF1ARequests%20acknowledgement%20that%20the%20write%20operations%20have%20propagated%20to%20a%20replica%20set%20member%20with%20the%20specified%20tag.%0A%3E%20-%20j%3A%20true%20%26%20false%2C%20%u51B3%u5B9A%u662F%u5426%u5199journal%0A%3E%20-%20wtimeout%3A%20This%20option%20specifies%20a%20time%20limit%2C%20in%20milliseconds%2C%20for%20the%20write%20concern.%20wtimeout%20is%20only%20applicable%20for%20w%20values%20greater%20than%201.%0A%0A%3EIf%20you%20specify%20w%3A%200%20but%20include%20j%3A%20true%2C%20the%20j%3A%20true%20prevails%20to%20request%20acknowledgement%20from%20the%20standalone%20mongod%20or%20the%20primary%20of%20a%20replica%20set.%0A%0A%23%23%23%20journal%0A%3E%20A%20sequential%2C%20binary%20transaction%20log%20used%20to%20bring%20the%20database%20into%20a%20valid%20state%20in%20the%20event%20of%20a%20hard%20shutdown.%20Journaling%20writes%20data%20first%20to%20the%20journal%20and%20then%20to%20the%20core%20data%20files.%20MongoDB%20enables%20journaling%20by%20default%20for%2064-bit%20builds%20of%20MongoDB%20version%202.0%20and%20newer.%20Journal%20files%20are%20pre-allocated%20and%20exist%20as%20files%20in%20the%20data%20directory.%20See%20%5BJournaling%5D%28https%3A//docs.mongodb.org/manual/core/journaling/%29.%0A%0A%23%23%23%20mongos%0A**%5Bmongos%5D%28https%3A//docs.mongodb.org/manual/reference/program/mongos/%29**%20for%20%u201CMongoDB%20Shard%2C%u201D%20is%20a%20routing%20service%20for%20MongoDB%20shard%20configurations%20that%20processes%20queries%20from%20the%20application%20layer%2C%20and%20determines%20the%20location%20of%20this%20data%20in%20the%20sharded%20cluster%2C%20in%20order%20to%20complete%20these%20operations.%20From%20the%20perspective%20of%20the%20application%2C%20a%20mongos%20instance%20behaves%20identically%20to%20any%20other%20MongoDB%20instance.%0A%0A%23%23mongos%20vs.%20mongod%0Amongodb%u91C7%u7528%u5C06%u96C6%u5408%u8FDB%u884C%u62C6%u5206%uFF0C%u7136%u540E%u5C06%u62C6%u5206%u7684%u6570%u636E%u5747%u644A%u5230%u51E0%u4E2A%u7247%u4E0A%u7684%u4E00%u79CD%u89E3%u51B3%u65B9%u6848%u3002%0A%21%5BAlt%20text%5D%28./1454111696209.png%29%0A%u7B11%u8138%u662F%u7528%u6237%uFF0Cconfig%u662Fconfig%u670D%u52A1%u5668%uFF0C%u4E5F%u662F%u4E00%u4E2Amongod%u7684%u6570%u636E%u5E93%0A%0A%23%23%u8FDE%u63A5mongo%u6570%u636E%u5E93%0A%u6211%u6536%u96C6%u4E86%u597D%u51E0%u7BC7%u8BB2%u5982%u4F55%u66F4%u597D%u7684%u8FDE%u63A5mongo%u6570%u636E%u5E93%u7684%u6587%u7AE0%u3002%u53C2%u8003%u4E0B%u9762%u7684%u7B14%u8BB0%uFF1A%0A%5Bmongodb%u9A71%u52A8%u7684%u6B63%u786E%u4F7F%u7528%u65B9%u6CD5%5D%28https%3A//www.evernote.com/shard/s24/nl/697376807/5bc8092d-bccf-40cd-9ad5-9ec0b28611dd/%29%0A%5Bmongodb%20%u8FDE%u63A5%u7684%u95EE%u9898%5D%28https%3A//www.evernote.com/shard/s24/nl/697376807/bb648a45-3206-4fb9-b0a4-e4024638f6f5/%29%0A%5Bnode-mongo-native1.3.19%u8FDE%u63A5mongo%u7684%u6700%u4F18%u65B9%u6CD5%5D%28https%3A//www.evernote.com/shard/s24/nl/697376807/eac602ff-a873-4d71-bc7b-0a5650905d76/%29%0A%u8FD8%u6709node.js%u7684MongoDB%20driverd%u7684%u5B98%u65B9%u4ECB%u7ECD%uFF1A%0A%5BMongoClient%20or%20how%20to%20connect%20in%20a%20new%20and%20better%20way%5D%28https%3A//www.evernote.com/shard/s24/nl/697376807/ee17ecea-566c-4f2f-8138-79cb8ea25d8f/%29%0A%u8FD8%u6709%u4EBA%u7528%u8FDE%u63A5%u6C60%u7684%0A%5B%u751F%u4EA7%u7EA7Nodejs%u5F00%u53D1%u5B9E%u8DF5-%u4F7F%u7528%u8FDE%u63A5%u6C60%5D%28https%3A//www.evernote.com/shard/s24/nl/697376807/53bb4dd3-fd7f-4780-93ae-67faee3637a5/%29%0A%23%23%23Old%20Style%0A%u5148%u770B%u4E00%u6BB5%u4EE3%u7801%0A%60%60%60javascript%0Avar%20server_options%3D%7B%7D%3B%0Avar%20db_options%3D%7Bw%3A-1%7D%3B%0A%0Avar%20mongodb%20%3D%20require%28%22mongodb%22%29%2C%0A%20%20%20%20mongoserver%20%3D%20new%20mongodb.Server%28%27localhost%27%2C%2027017%2Cserver_options%20%29%2C%0A%20%20%20%20db%20%3D%20new%20mongodb.Db%28%27test%27%2C%20mongoserver%2C%20db_options%29%3B%0A%0Avar%20http%3Drequire%28%27http%27%29%3B%0A%0Avar%20server%3Dhttp.createServer%28function%28req%2Cres%29%7B%0A%20%20%20%20db.open%28function%28err%2Cdb%29%7B%0A%20%20%20%20%20%20%20%20if%28err%29return%20console.error%28err%29%3B%0A%20%20%20%20%20%20%20%20console.log%28%27*%20mongodb%20connected%27%29%3B%0A%20%20%20%20%20%20%20%20db.collection%28%27foo%27%29.save%28%7Btest%3A1%7D%2Cfunction%28err%2Cresult%29%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20res.end%28JSON.stringify%28result%2Cnull%2C2%29%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20db.close%28%29%3B%0A%20%20%20%20%20%20%20%20%7D%29%3B%0A%20%20%20%20%7D%29%0A%0A%7D%29%3B%0A%60%60%60%0A%u6BCF%u6765%u4E00%u4E2Ahttp%u8BF7%u6C42%uFF0C%u5C31%u5F00%u5173%u4E00%u6B21%u6570%u636E%u5E93%u3002%u66F4%u641E%u7684%u662F%uFF0C%60new%20mongodb.Server%60%u7684%u53C2%u6570server_options%u91CC%u53EF%u4EE5%u6709%u4E00%u4E2ApoolSize%uFF0C%u5982%u679C%u4E0D%u6307%u5B9A%u7684%u8BDD%uFF0C%u9ED8%u8BA4%u503C%u5C31%u662F5%uFF0C%u4E5F%u5C31%u662F%u8BF4%u6BCF%u5F00%u4E00%u6B21%u6570%u636E%u5E93%uFF0C%u5B9E%u9645%u4E0A%u662F%u5F00%u4E865%u4E2A%u6570%u636E%u5E93%u8FDE%u63A5%u3002%u8FD9%u4E5F%u5C31%u662F%u4E3A%u4EC0%u4E48%u6211%u4EEC%u7ECF%u5E38%u5728%u6570%u636E%u5E93%u8FD0%u884C%u7EC8%u7AEF%u4E2D%u7ECF%u5E38%u81F3%u5C11%u770B%u89C15%u4E2Aconnections%u7684%u539F%u56E0%u3002%u8FD9%u4E5F%u662F%u4E3A%u4EC0%u4E48%u6211%u4EECconnections%u75AF%u6DA8%u7684%u539F%u56E0%u3002%0A%u53E6%u5916%u5728old%20style%u64CD%u4F5C%u4EE3%u7801%u4E2D%uFF0C%u8FD8%u6709%u4E00%u70B9%u503C%u5F97%u6CE8%u610F%u7684%u662F%uFF0Cclose%u662F%u4E0D%u4F1Aclose%u8BFB%u8FDE%u63A5%u7684%uFF0C%u5B83%u53EA%u4F1Aclose%u5199%u8FDE%u63A5%uFF0C%u8981%u5F3A%u5236close%u6240%u6709%u8FDE%u63A5%u8981%u7528close%28true%29%u3002%0A%3E**close**%0AClose%20the%20current%20db%20connection%2C%20including%20all%20the%20child%20db%20instances.%20Emits%20close%20event%20and%20calls%20optional%20callback.%0A%0Aclose%28%5B*forceClose*%5D%2C%20*callback*%29%0A**Arguments**%3A%09%0A*%20**%5BforceClose%5D**%20%28*boolean*%29%20%u2013%20connection%20can%20never%20be%20reused.%0A*%20**callback**%20%28*function*%29%20%u2013%20this%20will%20be%20called%20after%20executing%20this%20method.%20The%20first%20parameter%20will%20contain%20the%20Error%20object%20if%20an%20error%20occurred%2C%20or%20null%20otherwise.%20While%20the%20second%20parameter%20will%20contain%20the%20results%20or%20null%20if%20an%20error%20occurred.%0A%0A**Returns**%3A%09null%0A%23%23%23New%20Style%0A%u53C2%u8003%u5B98%u65B9%u4EE3%u7801%uFF1A%0A%60%60%60javascript%0Avar%20express%20%3D%20require%28%27express%27%29%3B%20%0Avar%20mongodb%20%3D%20require%28%27mongodb%27%29%3B%20var%20app%20%3D%20express%28%29%3B%20%0A%0Avar%20MongoClient%20%3D%20require%28%27mongodb%27%29.MongoClient%3B%20%0Avar%20db%3B%20%0A%0A//%20Initialize%20connection%20once%20MongoClient.connect%28%22mongodb%3A//localhost%3A27017/integration_test%22%2C%20%7B%20%0A%09db%3A%20%7B%20%0A%09%09native_parser%3A%20false%20%0A%09%7D%2C%20%0A%09server%3A%20%7B%20%0A%09%09socketOptions%3A%20%7B%20%0A%09%09%09connectTimeoutMS%3A%20500%20%0A%09%09%7D%20%0A%09%7D%2C%20%0A%09replSet%3A%20%7B%7D%2C%20%0A%09mongos%3A%20%7B%7D%20%0A%7D%2Cfunction%28err%2C%20database%29%20%7B%20%0A%09if%28err%29%20throw%20err%3B%20%0A%09db%20%3D%20database%3B%20%0A%0A%09//%20Start%20the%20application%20after%20the%20database%20connection%20is%20ready%20%0A%09app.listen%283000%29%3B%20%0A%09console.log%28%22Listening%20on%20port%203000%22%29%3B%20%0A%7D%29%3B%20%0A%0A//%20Reuse%20database%20object%20in%20request%20handlers%20%0Aapp.get%28%22/%22%2C%20function%28req%2C%20res%29%20%7B%20%09%09%09%0A%09db.collection%28%22replicaset_mongo_client_collection%22%29.find%28%7B%7D%2Cfunction%28err%2C%20docs%29%20%7B%20%0A%09%09docs.each%28function%28err%2C%20doc%29%20%7B%20%0A%09%09%09if%28doc%29%20%7B%20%0A%09%09%09%09console.log%28doc%29%3B%20%0A%09%09%09%7D%20%0A%09%09%09else%20%7B%0A%09%09%09%09res.end%28%29%3B%20%0A%09%09%09%7D%20%0A%09%09%7D%29%3B%0A%09%7D%29%3B%20%0A%7D%29%3B%0A%60%60%60%0A%23%23%23MongoClient.connect%20options%0A%u4E3B%u8981%u6709%u6765%u81EA4%u4E2A%u65B9%u9762%u7684%u8BBE%u7F6E%uFF1A%0A-%20db%0A-%20server%0A-%20replica%20set%0A-%20mongos%0A%u540E%u4E24%u4E2A%u9488%u5BF9sharding%uFF0C%u8FD9%u6B21%u6211%u4EEC%u4E3B%u8981%u5173%u6CE8%u524D%u4E24%u4E2A%u3002%u4ECE%u5B98%u65B9%u6587%u6863%u91CC%u6311%u51FA%u51E0%u6761%u914D%u7F6E%u7F57%u5217%u4E00%u4E0B%uFF1A%0A%23%23%23%23db%3A%20A%20hash%20of%20options%20at%20the%20db%20level%20overriding%20or%20adjusting%20functionality%20not%20supported%20by%20the%20url%0A-%20**w**%2C%20%7BNumber/String%2C%20%3E%20-1%20%7C%7C%20%u2018majority%u2019%7D%20the%20write%20concern%20for%20the%20operation%20where%20%3C%201%20is%20no%20acknowledgment%20of%20write%20and%20w%20%3E%3D%201%20or%20w%20%3D%20%u2018majority%u2019%20acknowledges%20the%20write%0A-%20wtimeout%2C%20%7BNumber%2C%200%7D%20set%20the%20timeout%20for%20waiting%20for%20write%20concern%20to%20finish%20%28combines%20with%20w%20option%29%0A-%20fsync%2C%20%28Boolean%2C%20default%3Afalse%29%20write%20waits%20for%20fsync%20before%20returning%0A-%20**journal**%2C%20%28Boolean%2C%20default%3Afalse%29%20write%20waits%20for%20journal%20sync%20before%20returning%0A-%20readPreference%20%7BString%7D%2C%20the%20preferred%20read%20preference%20%28ReadPreference.PRIMARY%2C%20ReadPreference.PRIMARY_PREFERRED%2C%20ReadPreference.SECONDARY%2C%20ReadPreference.SECONDARY_PREFERRED%2C%20ReadPreference.NEAREST%29.%0A-%20native_parser%20%7BBoolean%2C%20default%3Afalse%7D%2C%20use%20c++%20bson%20parser.%0A-%20forceServerObjectId%20%7BBoolean%2C%20default%3Afalse%7D%2C%20force%20server%20to%20create%20_id%20fields%20instead%20of%20client.%0A-%20pkFactory%20%7BObject%7D%2C%20object%20overriding%20the%20basic%20ObjectID%20primary%20key%20generation.%0A-%20serializeFunctions%20%7BBoolean%2C%20default%3Afalse%7D%2C%20serialize%20functions.%0A-%20raw%20%7BBoolean%2C%20default%3Afalse%7D%2C%20perform%20operations%20using%20raw%20bson%20buffers.%0A-%20recordQueryStats%20%7BBoolean%2C%20default%3Afalse%7D%2C%20record%20query%20statistics%20during%20execution.%0A-%20retryMiliSeconds%20%7BNumber%2C%20default%3A5000%7D%2C%20number%20of%20milliseconds%20between%20retries.%0A-%20numberOfRetries%20%7BNumber%2C%20default%3A5%7D%2C%20number%20of%20retries%20off%20connection.%0A-%20bufferMaxEntries%20%7BNumber%2C%20default%3A%20-1%7D%2C%20sets%20a%20cap%20on%20how%20many%20operations%20the%20driver%20will%20buffer%20up%20before%20giving%20up%20on%20getting%20a%20working%20connection%2C%20default%20is%20-1%20which%20is%20unlimited.%0A%23%23%23%23server%3A%20A%20hash%20of%20options%20at%20the%20server%20level%20not%20supported%20by%20the%20url.%0A-%20readPreference%20%7BString%2C%20default%3Anull%7D%2C%20set%u2019s%20the%20read%20preference%20%28ReadPreference.PRIMARY%2C%20ReadPreference.PRIMARY_PREFERRED%2C%20ReadPreference.SECONDARY%2C%20ReadPreference.SECONDARY_PREFERRED%2C%20ReadPreference.NEAREST%29%0A-%20ssl%20%7BBoolean%2C%20default%3Afalse%7D%2C%20use%20ssl%20connection%20%28needs%20to%20have%20a%20mongod%20server%20with%20ssl%20support%29%0A-%20slaveOk%20%7BBoolean%2C%20default%3Afalse%7D%2C%20legacy%20option%20allowing%20reads%20from%20secondary%2C%20use%20readPrefrence%20instead.%0A-%20**poolSize**%20%7BNumber%2C%20default%3A1%7D%2C%20number%20of%20connections%20in%20the%20connection%20pool%2C%20set%20to%201%20as%20default%20for%20legacy%20reasons.%0A-%20socketOptions%20%7BObject%2C%20default%3Anull%7D%2C%20an%20object%20containing%20socket%20options%20to%20use%20%28noDelay%3A%28boolean%29%2C%20keepAlive%3A%28number%29%2C%20connectTimeoutMS%3A%28number%29%2C%20socketTimeoutMS%3A%28number%29%29%0A-%20logger%20%7BObject%2C%20default%3Anull%7D%2C%20an%20object%20representing%20a%20logger%20that%20you%20want%20to%20use%2C%20needs%20to%20support%20functions%20debug%2C%20log%2C%20error%20%28%7Berror%3Afunction%28message%2C%20object%29%20%7B%7D%2C%20log%3Afunction%28message%2C%20object%29%20%7B%7D%2C%20debug%3Afunction%28message%2C%20object%29%20%7B%7D%7D%29.%0A-%20auto_reconnect%20%7BBoolean%2C%20default%3Afalse%7D%2C%20reconnect%20on%20error.%0A-%20disableDriverBSONSizeCheck%20%7BBoolean%2C%20default%3Afalse%7D%2C%20force%20the%20server%20to%20error%20if%20the%20BSON%20message%20is%20to%20big%0A%0A%u6211%u4EEC%u5173%u6CE8%u7684%u9009%u9879%uFF0C%u6211%u7528%u7C97%u4F53%u505A%u4E86%u6807%u6CE8%u3002%0A%0A%u53E6%u5916%u6211%u4EEC%u5728%u524D%u6587%u4E2D%u63D0%u5230%uFF0C%u6709%u4E9B%u6587%u7AE0%u63A8%u8350%u7528generic-pool%u4E3Amongo%u8FDE%u63A5%u63D0%u4F9B%u8FDE%u63A5%u6C60%u3002%u4F46%u662F%uFF0C%u6211%u53D1%u73B0%60MongoClient.connect%60%u7684option%u91CC%u5C31%u6709poolSize%u3002%u5728%u5B9E%u9645%u9879%u76EE%u4E2D%u76F4%u63A5%u5C06%u4E4B%u8D4B%u4E3A100%uFF0C%u4E5F%u53EF%u4EE5%u5F97%u5230%u4E0D%u9519%u7684%u6548%u679C%u3002%0A