Node.js连接示例

<p>本文主要介绍Node.js连接示例。</p> <p>&nbsp; &nbsp;&nbsp;<img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20202805101042-14cb996e9f2b.png" style="height:26px; margin:0px; width:65px" />:官方资料可参考<a href="http://mongodb.github.io/node-mongodb-native/2.2/quick-start/quick-start/" target="_blank">MongoDB Node.js Driver</a>。</p> <p><span style="font-size:18px"><strong>操作步骤</strong></span></p> <p>1.&nbsp; 执行以下命令,进行项目初始化。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa"> <p><span style="font-family:Consolas">mkdir <strong>node-mongodb-demo</strong></span></p> <p><span style="font-family:Consolas">cd <strong>node-mongodb-demo</strong></span></p> <p><span style="font-family:Consolas">npm init</span></p> </td> </tr> </tbody> </table> <p>2.&nbsp; 执行以下命令安装驱动包以及工具包。</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa"> <p><span style="font-family:Consolas"><code>npm install mongodb </code><strong>node</strong><strong>-uuid</strong><code> sprintf-js &ndash;save</code></span></p> </td> </tr> </tbody> </table> <p>3.&nbsp; 获取DDS实例的连接信息。</p> <p>4.&nbsp;&nbsp;Node.js连接代码:</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa"> <p><span style="font-family:Consolas">&#39;use strict&#39;;</span></p> <p><span style="font-family:Consolas"><strong>var</strong> uuid = <strong>require</strong>(&#39;node-uuid&#39;);</span></p> <p><span style="font-family:Consolas"><strong>var</strong> sprintf = <strong>require</strong>(&quot;sprintf-js&quot;).sprintf;</span></p> <p><span style="font-family:Consolas"><strong>var</strong> mongoClient = <strong>require</strong>(&#39;mongodb&#39;).MongoClient;</span></p> <p><span style="font-family:Consolas"><strong>var</strong> host = &quot;demotest.mongodb.db.cloud.papub&quot;;</span></p> <p><span style="font-family:Consolas"><strong>var</strong> port = 15923;</span></p> <p><span style="font-family:Consolas"><strong>var</strong> username = &quot;demouser&quot;;</span></p> <p><span style="font-family:Consolas"><strong>var</strong> password = &quot;123456&quot;;</span></p> <p><span style="font-family:Consolas"><strong>var</strong> replSetName = &quot;demotest&quot;;</span></p> <p><span style="font-family:Consolas"><strong>var</strong> demoDb = &quot;test&quot;;</span></p> <p><span style="font-family:Consolas"><strong>var</strong> demoColl = &quot;testColl&quot;;</span></p> <p><span style="font-family:Consolas">// 官方建议使用的方案</span></p> <p><span style="font-family:Consolas"><strong>var</strong> url = sprintf(&quot;mongodb://%s:%d/%s?replicaSet=%s&quot;, host, port, demoDb, replSetName);</span></p> <p><span style="font-family:Consolas"><strong>console</strong>.info(&quot;url:&quot;, url);</span></p> <p><span style="font-family:Consolas">//获取mongoClient</span></p> <p><span style="font-family:Consolas">mongoClient.connect(url, <strong>function</strong> (err, db) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; <strong>if</strong> (err) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>console</strong>.error(&quot;connect err:&quot;, err);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>return</strong> 1;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; //授权. 这里的username基于test数据库授权</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; <strong>var</strong> adminDb = db.admin();</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; adminDb.authenticate(username, password, <strong>function</strong> (err, result) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>if</strong> (err) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>console</strong>.error(&quot;authenticate err:&quot;, err);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>return</strong> 1;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //取得Collecton句柄</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>var</strong> collection = db.collection(demoColl);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>var</strong> demoName = &quot;NODE:&quot; + uuid.v1();</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>var</strong> doc = { &quot;DEMO&quot;: demoName, &quot;MESG&quot;: &quot;Hello AliCoudDB For MongoDB&quot; };</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>console</strong>.info(&quot;ready insert document: &quot;, doc);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 插入数据</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; collection.insertOne(doc, <strong>function</strong> (err, data) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>if</strong> (err) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>console</strong>.error(&quot;insert err:&quot;, err);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>return</strong> 1;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>console</strong>.info(&quot;insert result:&quot;, data[&quot;result&quot;]);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 读取数据</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>var</strong> filter = { &quot;DEMO&quot;: demoName };</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; collection.find(filter).toArray(<strong>function</strong> (err, items) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>if</strong> (err) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>console</strong>.error(&quot;find err:&quot;, err);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>return</strong> 1;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>console</strong>.info(&quot;find document: &quot;, items);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //关闭Client,释放资源</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; db.close();</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; });</span></p> <p><span style="font-family:Consolas">});</span></p> </td> </tr> </tbody> </table>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

工单支持

解决云产品相关技术问题