Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement