node????mongodb????????
???????????? ???????[ 2016/9/21 11:08:07 ] ????????????? mongodb
????????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????
*/
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11