MongoDB Series 3 MONGO Mongoskin connection and number of connections questions advanced

Source: Internet
Author: User
Tags connect mongo mongodb driver

1) Connect MONGO with MongoDB

varMONGO = require (' MongoDB '),//Introducing MongoDB Dbhost= ' 127.0.0.1 ', Dbport= 27017;//Configure basic informationvarDb =MONGO. Db; varConnection =MONGO. Connection;varServer =MONGO. Server;vardb =NewDb (' Local ',NewServer (Dbhost, Dbport), {safe:true}); Initializes the database object Db.open (function(Error, dbConnection) {if(Error) {Console.error (error); Process.exit (1); }  varitem ={name:"Haha"  }; Dbconnection.collection ("AA"). Insert (item,function(error,item) {if(Error) {Console.error (error); Process.exit (1); } console.log ("Inserted");    Db.close (); Process.exit (0); Insert a piece of data into the collection AA (AA is created without a presence)}) ;

See success inserting the data I created in the program in collection AA

2) Connect MONGO with Mongoskin

/** * Created by Hao on 2016/4/19.*/varMongoskin = require ("Mongoskin"), Dbhost= "127.0.0.1", Dbport= 27017;vardb = mongoskin.db (Dbhost + ': ' + dbport + '/local ', {safe:true}); Basic Database Configuration Db.bind ("AA", {findoneandaddage:function(AGE,FN) {db.collection ("AA"). FindOne ({},function(error,item) {if(Error) {Console.log (error); Process.exit (1); } item.age=Age ; Db.collection ("AA"). Save (item,function(error,item) {if(Error) {Console.error (error); Process.exit (1); } console.log ("Saved");      Db.close ();    });  }); }}); To create a method custom method for a data collection here, when the corresponding collection does not exist in the database, the following method of calling the binding will make an error compared to the MongoDB connection condition Db.collection ("AA"). Findoneandaddage (22,function() {Console.log ("Changed");});

Modified the data

Tip:mongoskin is a subset of node. JS Native Drive MongoDB and you can also use Mongoskin as you would with MongoDB

3) Number of advanced connections

/** * Created by Hao on 2016/4/19.*/varServer_options = {};varDb_options ={w:-1, logger:{log:function(msg,obj) {Console.log (' [log] ' +msg); }  }}; Open thevarMongoDB = require ("MongoDB"), Mongoserver=NewMongodb. Server ("localhost", 27017, server_options),//Initialize server DB according to Server_options=NewMongodb. Db (' Local ', Mongoserver,{safe:true});functionTest () {Db.open (function(error,dbcollection) {if(Error) {Console.errro (error); Process.exit (1); } dbcollection.collection ("AA"). Insert ({name: "Hao"},function(error,item) {if(Error) {Console.error (error); Process.exit (1); } console.log ("Inserted");    Db.close ();  }); });} Test ();

There is a poolsize option in Server_options the default value is 5 db to provide this connection pool by default every time the request comes over it will open the connection pool with 5 connections, then close the connection pool, that is, each request comes up with 5 connections and then closes 5 connections

There is a problem with the above pattern, when the number of visits surges, it will appear before the connection is not closed, the request to open this has not shut down the request error can be switched below this mode is when the program starts the Open database connection, and then do not close the connection after the operation, However, there is a certain problem with this mode, that is, when the number of concurrent accesses is large, only 5 of the database connections available can be blocked the solution is to use the connection pool object pattern

Please refer to this article for more information on the connection number is from the above https://cnodejs.org/topic/5190d61263e9f8a542acd83b MongoDB driver's correct use method

MongoDB Series 3 MONGO Mongoskin connection and number of connections questions advanced

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.