Skip to content

Instantly share code, notes, and snippets.

@toandaominh1997
Last active June 17, 2020 13:35
Show Gist options
  • Save toandaominh1997/11a1282099746eb0ad6240e39ef4cc85 to your computer and use it in GitHub Desktop.
Save toandaominh1997/11a1282099746eb0ad6240e39ef4cc85 to your computer and use it in GitHub Desktop.
class DBMongo{
constructor(url){
this.url = url
this.MongoClient = require('mongodb').MongoClient;
}
createcollection(clt="hardware", dbase="mydb"){
this.MongoClient.connect(this.url, function(err, db){
if(err) throw err;
var dbo = db.db(dbase)
dbo.createCollection(clt, function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close();
});
})
}
dropcollection(clt="hardware", dbase="mydb"){
this.MongoClient.connect(this.url, function(err, db){
if(err) throw err;
var dbo = db.db(dbase)
dbo.collection(clt).drop(function(err, delOK) {
if (err) throw err;
if (delOK) console.log("Collection deleted");
db.close();
});
})
}
insertOne(data, clt="hardware", dbase="mydb"){
this.MongoClient.connect(this.url, function(err, db){
if(err) throw err;
var dbo = db.db(dbase)
dbo.collection(clt).insertOne(data, function(err, res){
if(err) throw err;
console.log("Insert One");
db.close();
})
})
}
insertMany(data, clt="hardware", dbase="mydb"){
this.MongoClient.connect(this.url, function(err, db){
if(err) throw err;
var dbo = db.db(dbase)
dbo.collection(clt).insertMany(data, function(err, res){
if(err) throw err;
console.log("Number of documents inserted: " + res.insertedCount);
db.close();
})
})
}
findAll(clt="hardware", dbase="mydb"){
this.MongoClient.connect(this.url, function(err, db){
if(err) throw err;
var dbo = db.db(dbase)
dbo.collection(clt).find({}).toArray(function(err, res){
if(err) throw err;
console.log("result of findAll: ", res);
db.close();;
;})
;})
}
query(data, clt="customers", dbase="mydb"){
this.MongoClient.connect(this.url, function(err, db){
if(err) throw err;
var dbo = db.db(dbase)
dbo.collection(clt).find(data).toArray(function(err, res){
if(err) throw err;
console.log("result of query: ", res);
db.close();
})
})
}
deleteOne(data, clt="hardware", dbase="mydb"){
this.MongoClient.connect(this.url, function(err, db){
if(err) throw err;
var dbo = db.db(dbase);
dbo.collection(clt).deleteOne(data, function(err, res){
if(err) throw err;
console.log("result: ", res);
db.close();
})
})
}
deleteMany(data, clt="hardware", dbase="mydb"){
this.MongoClient.connect(this.url, function(err, db){
if(err) throw err;
var dbo = db.db(dbase);
dbo.collection(clt).deleteMany(data, function(err, obj){
if(err) throw err;
console.log(obj.result.n + " document(s) deleted");
db.close();
})
})
}
updateOne(olddata, newdata, clt="customers", dbase="mydb"){
this.MongoClient.connect(this.url, function(err, db){
if(err) throw err;
var dbo = db.db(dbase);
var newvalue = {$set: newdata};
dbo.collection(clt).updateOne(olddata, newvalue, function(err, res){
if(err) throw err;
console.log("1 document updated");
db.close();
})
})
}
updateMany(olddata, newdata, clt="customers", dbase="mydb"){
this.MongoClient.connect(this.url, function(err, db){
if(err) throw err;
var dbo = db.db(dbase);
var newvalue = {$set: newdata};
dbo.collection(clt).updateMany(olddata, newvalue, function(err, res){
if(err) throw err;
console.log(res.result.nModified + " document(s) updated");
db.close();
})
})
}
}
var url = "mongodb://localhost:27017";
mongo = new DBMongo(url=url);
// create collection
mongo.createcollection(clt="hardware")
mongo.insertOne(data={name: "toan", address: "123" }, clt="hardware");
mongo.insertMany(data=[{name: "kaka", address: "456" },
{name: "weihan", address: "789"}
], clt="hardware");
//mongo.updateMany(olddata={name: "kaka"}, newdata={name: "hung", address: "101112"}, clt="hardware");
//mongo.findAll(clt="hardware");
//mongo.deleteOne(data={name: "weihan"}, clt="hardware");
mongo.findAll(clt="hardware")
//mongo.query(data={name:"toan"})
//mongo.dropcollection(clt="hardware")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment