|
|
@@ -0,0 +1,60 @@
|
|
|
+//包含mongodb驱动程序
|
|
|
+var mongodb = require("mongodb");
|
|
|
+//mongodb安装位置
|
|
|
+var server = new mongodb.Server('192.168.65.17', 27017, { auto_reconnect: true });
|
|
|
+//指定数据库
|
|
|
+var db = new mongodb.Db('UseStudio_Blog', server, { safe: true });
|
|
|
+
|
|
|
+//打开数据库,console.log('connect');输出调试信息到控制台
|
|
|
+db.open(function (err, db) { if (!err) { console.log('connect'); } else { console.log(err); } });
|
|
|
+
|
|
|
+//选取数据的示例
|
|
|
+exports.select = function (sql, callback) {
|
|
|
+ db.collection('dbo.PBDirectory', { safe: true }, function (err, collection) {
|
|
|
+ //为啥monogodb返回多条数据不是Json。有点想不通,要先转数组,再转json。而选取一条数据好像不用
|
|
|
+ //collection.find(function (err, _json) { callback(_json); });
|
|
|
+ //查找所有数据。
|
|
|
+ //console.log(collection);
|
|
|
+ collection.find().toArray(function (err, _json) { callback(JSON.stringify(_json)); })
|
|
|
+ //根据条件选取数据
|
|
|
+ //collection.find({ "UserDirectoryName": "自然岩壁" }).toArray(function (err, _json) { callback(JSON.stringify(_json)); })
|
|
|
+ //根据条件只选取一条数据。为啥取出来的还不是json,还需要转换为json?mongdb需要改写。
|
|
|
+ //collection.findOne({ "UserDirectoryName": "自然岩壁" }, function (err, _json) { callback(JSON.stringify(_json)); })
|
|
|
+ });
|
|
|
+}
|
|
|
+//存储过程示例
|
|
|
+exports.pro = function (sql, callback) {
|
|
|
+ db.eval("testpro()",function (err, _json) {
|
|
|
+ console.log(_json);
|
|
|
+ callback(JSON.stringify(_json));
|
|
|
+ });
|
|
|
+}
|
|
|
+//存储过程示例,带参数
|
|
|
+exports.testparam = function (sql, callback) {
|
|
|
+ var _param = "自然岩壁";
|
|
|
+ console.log(_param);
|
|
|
+ //eval的参数传递要注意,传递字符用单引号。传递字符变量用'"++"'。有点纠结。不用单引号eval出来是个对象。所以一定要单引号
|
|
|
+ //db.eval("testparam('自然岩壁')", function (err, _json) {
|
|
|
+ db.eval("testparam('"+_param+"')", function (err, _json) {
|
|
|
+ console.log(_json);
|
|
|
+ callback(JSON.stringify(_json));
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+//封装数据库连接,暂时不做。以后再看
|
|
|
+/*BlogProvider = function (host, port, database) {
|
|
|
+ //指定服务器
|
|
|
+ this.server = new mongodb.Server(host, port, { safe: false }, { auto_reconnect: true }, {});
|
|
|
+ //指定数据库
|
|
|
+ this.db = new mongodb.Db(database, this.server);
|
|
|
+ //打开连接
|
|
|
+ this.db.open(function (err, db) { if (!err) { console.log('connect'); } else { console.log(err); } });
|
|
|
+}
|
|
|
+BlogProvider.prototype.proselect = function (sql, callback) {
|
|
|
+db.eval("testpro()", function (err, _json) {console.log(_json);callback(JSON.stringify(_json));});}
|
|
|
+exports.BlogProvider = BlogProvider;*/
|
|
|
+
|
|
|
+
|
|
|
+
|