Node.js连接示例
<p>本文主要介绍Node.js连接示例。</p>
<p> <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. 执行以下命令,进行项目初始化。</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. 执行以下命令安装驱动包以及工具包。</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 –save</code></span></p>
</td>
</tr>
</tbody>
</table>
<p>3. 获取DDS实例的连接信息。</p>
<p>4. Node.js连接代码:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">'use strict';</span></p>
<p><span style="font-family:Consolas"><strong>var</strong> uuid = <strong>require</strong>('node-uuid');</span></p>
<p><span style="font-family:Consolas"><strong>var</strong> sprintf = <strong>require</strong>("sprintf-js").sprintf;</span></p>
<p><span style="font-family:Consolas"><strong>var</strong> mongoClient = <strong>require</strong>('mongodb').MongoClient;</span></p>
<p><span style="font-family:Consolas"><strong>var</strong> host = "demotest.mongodb.db.cloud.papub";</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 = "demouser";</span></p>
<p><span style="font-family:Consolas"><strong>var</strong> password = "123456";</span></p>
<p><span style="font-family:Consolas"><strong>var</strong> replSetName = "demotest";</span></p>
<p><span style="font-family:Consolas"><strong>var</strong> demoDb = "test";</span></p>
<p><span style="font-family:Consolas"><strong>var</strong> demoColl = "testColl";</span></p>
<p><span style="font-family:Consolas">// 官方建议使用的方案</span></p>
<p><span style="font-family:Consolas"><strong>var</strong> url = sprintf("mongodb://%s:%d/%s?replicaSet=%s", host, port, demoDb, replSetName);</span></p>
<p><span style="font-family:Consolas"><strong>console</strong>.info("url:", 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"> <strong>if</strong> (err) {</span></p>
<p><span style="font-family:Consolas"> <strong>console</strong>.error("connect err:", err);</span></p>
<p><span style="font-family:Consolas"> <strong>return</strong> 1;</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> //授权. 这里的username基于test数据库授权</span></p>
<p><span style="font-family:Consolas"> <strong>var</strong> adminDb = db.admin();</span></p>
<p><span style="font-family:Consolas"> adminDb.authenticate(username, password, <strong>function</strong> (err, result) {</span></p>
<p><span style="font-family:Consolas"> <strong>if</strong> (err) {</span></p>
<p><span style="font-family:Consolas"> <strong>console</strong>.error("authenticate err:", err);</span></p>
<p><span style="font-family:Consolas"> <strong>return</strong> 1;</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> //取得Collecton句柄</span></p>
<p><span style="font-family:Consolas"> <strong>var</strong> collection = db.collection(demoColl);</span></p>
<p><span style="font-family:Consolas"> <strong>var</strong> demoName = "NODE:" + uuid.v1();</span></p>
<p><span style="font-family:Consolas"> <strong>var</strong> doc = { "DEMO": demoName, "MESG": "Hello AliCoudDB For MongoDB" };</span></p>
<p><span style="font-family:Consolas"> <strong>console</strong>.info("ready insert document: ", doc);</span></p>
<p><span style="font-family:Consolas"> // 插入数据</span></p>
<p><span style="font-family:Consolas"> collection.insertOne(doc, <strong>function</strong> (err, data) {</span></p>
<p><span style="font-family:Consolas"> <strong>if</strong> (err) {</span></p>
<p><span style="font-family:Consolas"> <strong>console</strong>.error("insert err:", err);</span></p>
<p><span style="font-family:Consolas"> <strong>return</strong> 1;</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> <strong>console</strong>.info("insert result:", data["result"]);</span></p>
<p><span style="font-family:Consolas"> // 读取数据</span></p>
<p><span style="font-family:Consolas"> <strong>var</strong> filter = { "DEMO": demoName };</span></p>
<p><span style="font-family:Consolas"> collection.find(filter).toArray(<strong>function</strong> (err, items) {</span></p>
<p><span style="font-family:Consolas"> <strong>if</strong> (err) {</span></p>
<p><span style="font-family:Consolas"> <strong>console</strong>.error("find err:", err);</span></p>
<p><span style="font-family:Consolas"> <strong>return</strong> 1;</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> <strong>console</strong>.info("find document: ", items);</span></p>
<p><span style="font-family:Consolas"> //关闭Client,释放资源</span></p>
<p><span style="font-family:Consolas"> db.close();</span></p>
<p><span style="font-family:Consolas"> });</span></p>
<p><span style="font-family:Consolas"> });</span></p>
<p><span style="font-family:Consolas"> });</span></p>
<p><span style="font-family:Consolas">});</span></p>
</td>
</tr>
</tbody>
</table>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!