????????node ?????????????????????????????mongodb???????????????????????????????????????????????????????????????????????????
????????????mongodb???
????var mongodb = require('mongodb'); // ????mongodb????
????????????????????
????var dataconfig = require('../config/config'); // ????mongodb????
????????????????
??????????????????????????????
/* @dataconfig ?????????*/
var dataconfig={
dataurl:"localhost"??
dataport:'27017'??
dataname:'db'
};
module.exports = dataconfig;
??????????????????д??????????????
/*
????mongodb????????
*/
var Mainpulation = {
/*
@selectall ???????????????????
@dataname  ?????????
@dealdata   ???????????  ???function(result){};
*/
selectall: function(dataname?? dealdata) {
var server = new mongodb.Server(dataconfig.dataurl?? dataconfig.dataport?? {
auto_reconnect: true
}); // ????mongodb????
var db = new mongodb.Db(dataconfig.dataname?? server?? {
safe: true
});
db.open(function(err?? db) {
if (err) {
console.log('?????????');
} else {
db.createCollection(dataname?? {
safe: true
}?? function(err?? collection) {
if (err) {
console.log('????????????');
} else {
//  var tmp1 = {title:'hellodsad'};
//  var tmp2 = {title:'worlddsad'};
//  collection.insert([tmp1??tmp2]??{safe:true}??function(err??result){
//          console.log(result);
//  });
collection.find().toArray(function(err?? docs) {
console.log('find');
dealdata(docs);
db.close();
});
}
})
}
})
db.on("close"?? function(err?? data) {
if (err) {
console.log("??????????");
}
console.log('???????????');
});
}??
/*
@selectone ?????????????????
@dataname ?????????
@selectlanguage ??????????  ???{index:value??index??value};
@dealdata   ??????????  ???function(result){};
*/
select: function(dataname?? selectlanguage?? dealdata) {
var server = new mongodb.Server(dataconfig.dataurl?? dataconfig.dataport?? {
auto_reconnect: true
}); // ????mongodb????
var db = new mongodb.Db(dataconfig.dataname?? server?? {
safe: true
});
db.open(function(err?? db) {
if (err) {
console.log('?????????');
} else {
db.createCollection(dataname?? {
safe: true
}?? function(err?? collection) {
if (err) {
console.log('????????????');
} else {
collection.find(selectlanguage).toArray(function(err?? docs) {
console.log('find');
dealdata(docs);
db.close();
});
}
})
}
})
db.on("close"?? function(err?? data) {
if (err) {
console.log("??????????");
}
console.log('???????????');
});
}??
/*
@insert?????????json???
@dataname ?????????
@dealdata ???????????????????result????
*/
insert: function(dataname?? insertlanguage?? dealdata) {
var server = new mongodb.Server(dataconfig.dataurl?? dataconfig.dataport?? {
auto_reconnect: true
}); // ????mongodb????
var db = new mongodb.Db(dataconfig.dataname?? server?? {
safe: true
});
db.open(function(err?? db) {
if (err) {
console.log('?????????');
} else {
db.createCollection(dataname?? {
safe: true
}?? function(err?? collection) {
if (err) {
console.log('????????????');
} else {
collection.insert(insertlanguage?? {
safe: true
}?? function(err?? result) {
console.log(result+'??????');
dealdata(result);
db.close();
});
}
})
}
})
db.on("close"?? function(err?? data) {
if (err) {
console.log("??????????");
}
console.log('???????????');
});
}??
/*
@update ???????????
@update?????????json???
@dataname ?????????
@dealdata ???????????????????result????
*/
update: function(dataname?? updatelanguage??updatecondition?? dealdata) {
var server = new mongodb.Server(dataconfig.dataurl?? dataconfig.dataport?? {
auto_reconnect: true
}); // ????mongodb????
var db = new mongodb.Db(dataconfig.dataname?? server?? {
safe: true
});
db.open(function(err?? db) {
if (err) {
console.log('?????????');
} else {
db.createCollection(dataname?? {
safe: true
}?? function(err?? collection) {
if (err) {
console.log('????????????');
} else {
collection.update(updatecondition??updatelanguage?? {
safe: true
}?? function(err?? result) {
console.log(result+'?????');
dealdata(result);
db.close();
});
}
})
}
})
db.on("close"?? function(err?? data) {
if (err) {
console.log("??????????");
}
console.log('???????????');
});
}??
/*
@remove  ???????????
@dataname ?????????
@removelanguage ????????????
@dealdata ???????????????????result????
*/