nodejs win下没有npm功能,应用各种库文件相当杯具,这里用了node-mongodb-native在win下操作mongodb,小D很菜,对nodejs各种不懂,勉强简单封装了个对mongodb的curd操作,,,,
首先去下载https://github.com/christkv/node-mongodb-native,把文件lib下的东西放到了如下目录
如何操作看他的文档吧,e文不行的估计看例子也能看个大概….下面是封装代码mghp.js
1 |
<span style="line-height:1.5;color:#0000ff;">var</span><span style="line-height:1.5;color:#000000;"> mongodb </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"> require(</span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">./refLib/mongodb</span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">);<br /><br /></span><span style="line-height:1.5;color:#0000ff;">var</span><span style="line-height:1.5;color:#000000;"> _server </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(mongoCollection, mongoDB) {<br /></span><span style="line-height:1.5;color:#0000ff;">var</span><span style="line-height:1.5;color:#000000;"> sv </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">new</span><span style="line-height:1.5;color:#000000;"> mongodb.Server(</span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">127.0.0.1</span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">, </span><span style="line-height:1.5;color:#000000;">27017</span><span style="line-height:1.5;color:#000000;">, {});<br /></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">.mongoCollection </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"> mongoCollection </span><span style="line-height:1.5;color:#000000;">||</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">nodeCollection</span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">;<br /></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">.mongoDB </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"> mongoDB </span><span style="line-height:1.5;color:#000000;">||</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">nodeDB</span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">;<br /></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">.db </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">new</span><span style="line-height:1.5;color:#000000;"> mongodb.Db(</span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">.mongoDB, sv, {});<br />}<br /><br />_server.prototype.Open </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(callback) {<br /></span><span style="line-height:1.5;color:#0000ff;">var</span><span style="line-height:1.5;color:#000000;"> that </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">;<br /></span><span style="line-height:1.5;color:#0000ff;">if</span><span style="line-height:1.5;color:#000000;"> (that.db </span><span style="line-height:1.5;color:#000000;">&&</span><span style="line-height:1.5;color:#000000;"> that.db.state </span><span style="line-height:1.5;color:#000000;">==</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">connected</span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">) {<br /> callback </span><span style="line-height:1.5;color:#000000;">&&</span><span style="line-height:1.5;color:#000000;"> callback();<br /> } </span><span style="line-height:1.5;color:#0000ff;">else</span><span style="line-height:1.5;color:#000000;"> {<br /> that.db.open(</span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(error, client) {<br /></span><span style="line-height:1.5;color:#0000ff;">if</span><span style="line-height:1.5;color:#000000;"> (error) </span><span style="line-height:1.5;color:#0000ff;">throw</span><span style="line-height:1.5;color:#000000;"> error;<br /> that.collection </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">new</span><span style="line-height:1.5;color:#000000;"> mongodb.Collection(client, that.mongoCollection);<br /> callback </span><span style="line-height:1.5;color:#000000;">&&</span><span style="line-height:1.5;color:#000000;"> callback();<br /> });<br /> }<br />}<br /><br />_server.prototype.Close </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(isClose) {<br /></span><span style="line-height:1.5;color:#0000ff;">if</span><span style="line-height:1.5;color:#000000;"> (</span><span style="line-height:1.5;color:#000000;">!!</span><span style="line-height:1.5;color:#000000;">isClose </span><span style="line-height:1.5;color:#000000;">&&</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">.db) {<br /></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">.db.close();<br /></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">.collection </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">null</span><span style="line-height:1.5;color:#000000;">;<br /></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">.db </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">null</span><span style="line-height:1.5;color:#000000;">;<br /> }<br />}<br /><br />_server.prototype.Insert </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(obj, func, isClose) {<br /></span><span style="line-height:1.5;color:#0000ff;">var</span><span style="line-height:1.5;color:#000000;"> that </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">;<br /> that.Open(</span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">() {<br /> that.collection.insert(obj, { safe: </span><span style="line-height:1.5;color:#0000ff;">true</span><span style="line-height:1.5;color:#000000;"> }, </span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(err) {<br /> that.Close(isClose);<br /></span><span style="line-height:1.5;color:#0000ff;">if</span><span style="line-height:1.5;color:#000000;"> (err) {<br /> console.warn(err.message);<br /> }<br /></span><span style="line-height:1.5;color:#0000ff;">if</span><span style="line-height:1.5;color:#000000;"> (err </span><span style="line-height:1.5;color:#000000;">&&</span><span style="line-height:1.5;color:#000000;"> err.message.indexOf(</span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">E11000</span><span style="line-height:1.5;color:#000000;">'</span><span style="line-height:1.5;color:#000000;">) </span><span style="line-height:1.5;color:#000000;">!==</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#000000;">-</span><span style="line-height:1.5;color:#000000;">1</span><span style="line-height:1.5;color:#000000;">) {<br /></span><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;">this _id was already inserted in the database</span><span style="line-height:1.5;color:#008000;"><br /></span><span style="line-height:1.5;color:#000000;"> }<br /> func </span><span style="line-height:1.5;color:#000000;">&&</span><span style="line-height:1.5;color:#000000;"> func();<br /> });<br /> });<br />}<br /><br /><br />_server.prototype.Find </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(obj, func, isClose) {<br /></span><span style="line-height:1.5;color:#0000ff;">var</span><span style="line-height:1.5;color:#000000;"> that </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">;<br /> that.Open(</span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">() {<br /> that.collection.find(obj, </span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(err, cursor) {<br /> that.Close(isClose);<br /></span><span style="line-height:1.5;color:#0000ff;">if</span><span style="line-height:1.5;color:#000000;"> (err) {<br /> console.warn(err.message);<br /> }<br /> cursor.toArray(</span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(err, items) {<br /> func </span><span style="line-height:1.5;color:#000000;">&&</span><span style="line-height:1.5;color:#000000;"> func(items);<br /> });<br /><br /> });<br /> });<br />}<br /><br />_server.prototype.Update </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(obj, objN, func, isClose) {<br /></span><span style="line-height:1.5;color:#0000ff;">var</span><span style="line-height:1.5;color:#000000;"> that </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">;<br /> that.Open(</span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">() {<br /> that.collection.update(obj, { $set: objN }, { safe: </span><span style="line-height:1.5;color:#0000ff;">true</span><span style="line-height:1.5;color:#000000;"> }, </span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(err) {<br /> that.Close(isClose);<br /></span><span style="line-height:1.5;color:#0000ff;">if</span><span style="line-height:1.5;color:#000000;"> (err) {<br /> console.warn(err.message);<br /> }<br /> func </span><span style="line-height:1.5;color:#000000;">&&</span><span style="line-height:1.5;color:#000000;"> func();<br /> });<br /> });<br />}<br /><br />_server.prototype.Remove </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(obj, func, isClose) {<br /></span><span style="line-height:1.5;color:#0000ff;">var</span><span style="line-height:1.5;color:#000000;"> that </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"></span><span style="line-height:1.5;color:#0000ff;">this</span><span style="line-height:1.5;color:#000000;">;<br /> that.Open(</span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">() {<br /> that.collection.remove(obj, </span><span style="line-height:1.5;color:#0000ff;">function</span><span style="line-height:1.5;color:#000000;">(err, result) {<br /> that.Close(isClose);<br /></span><span style="line-height:1.5;color:#0000ff;">if</span><span style="line-height:1.5;color:#000000;"> (err) {<br /> console.warn(err.message);<br /> }<br /> func </span><span style="line-height:1.5;color:#000000;">&&</span><span style="line-height:1.5;color:#000000;"> func(result);<br /> });<br /> });<br />}<br /><br />exports.Server </span><span style="line-height:1.5;color:#000000;">=</span><span style="line-height:1.5;color:#000000;"> _server;</span> |
因为是异步的所以连续操作的话要放在回调函数中,方法还有第三个参数,如果是true则关闭链接
s.Insert({ "ID": "10001", "name": "ygm" }, function() {
….
},true);
查看操作结果
转自:http://www.cnblogs.com/ygm125/archive/2011/09/02/2163532.html