Skip to content

Instantly share code, notes, and snippets.

@oximenvn
Last active October 4, 2019 01:00
Show Gist options
  • Select an option

  • Save oximenvn/700c44dea5839e3595f330d31e45fc50 to your computer and use it in GitHub Desktop.

Select an option

Save oximenvn/700c44dea5839e3595f330d31e45fc50 to your computer and use it in GitHub Desktop.
Mongo-Nodejs driver demo
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://thanh:123456@localhost:27017/mydb';
// Database Name
const dbName = 'mydb';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
//insertdocument
var data = [
{a : 1}, {a : 2}, {a : 3}
];
//insertDocuments(db, "tbl_setting", data, function callback(result){
// console.log(result);
// });
// Find
findDocuments(db,"tbl_setting", {a:2},{ a: 0}, function callback(result){
console.log(result);
});
//Update
var new_value = {$set:{text:"Updated by thanhnt."}};
updateDocuments(db, "tbl_setting", {a: 2}, new_value, function callback(result){
//console.log(result);
});
//Delete
deleteDocuments(db, "tbl_setting", {a: 1}, function callback(result){
//console.log(result);
});
//Aggregate
var lookup =[{
$lookup:{
from: 'documents',
localField: 'a',
foreignField: 'a',
as: "on_document"
}
}];
joinDocuments(db, "tbl_setting", lookup, function callback(result){
console.log(JSON.stringify(result, 2));
});
client.close();
});
const insertDocuments = function(db, table, data, callback) {
// Get the documents collection
const collection = db.collection(table);
// Insert some documents
collection.insertMany(data, function(err, result) {
assert.equal(err, null);
//assert.equal(3, result.result.n);
//assert.equal(3, result.ops.length);
console.log("Inserted "+ result.result.n +" documents into the collection");
callback(result);
});
}
const findDocuments = function(db, table, condition, projection, callback){
const collection = db.collection(table);
// Find documents
collection.find(condition, { "projection": projection}).toArray(function abc(err, docs){
assert.equal(err, null);
console.log("Found the following records");
//console.log(docs);
callback(docs);
});
}
const deleteDocuments = function(db, table, condition, callback){
const collection = db.collection(table);
// Delete documents
collection.deleteMany(condition, function(err, result) {
assert.equal(err, null);
//assert.equal(3, result.result.n);
//assert.equal(3, result.ops.length);
console.log("Deleted "+ result.result.n +" documents into the collection");
callback(result);
});
}
const updateDocuments = function(db, table, condition, new_value, callback){
const collection = db.collection(table);
// Update documents
collection.updateMany(condition, new_value, function(err, result){
assert.equal(err, null);
console.log("Updated "+ result.result.nModified +" documents into the collection");
callback(result);
});
}
const joinDocuments = function(db, table, join_field, callback ){
const collection = db.collection(table);
//Join documents
collection.aggregate(join_field).toArray(function abc(err, docs){
assert.equal(err, null);
console.log("Found the following records");
//console.log(docs);
callback(docs);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment