Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. const MongoClient = require('mongodb').MongoClient;
  2. const assert = require('assert');
  3.  
  4. // Connection URL
  5. const url = 'mongodb://thanh:123456@localhost:27017/mydb';
  6.  
  7. // Database Name
  8. const dbName = 'mydb';
  9.  
  10. // Use connect method to connect to the server
  11. MongoClient.connect(url, function(err, client) {
  12. assert.equal(null, err);
  13. console.log("Connected successfully to server");
  14.  
  15. const db = client.db(dbName);
  16.  
  17. //insertdocument
  18. var data = [
  19. {a : 1}, {a : 2}, {a : 3}
  20. ];
  21. //insertDocuments(db, "tbl_setting", data, function callback(result){
  22. // console.log(result);
  23. // });
  24.  
  25. // Find
  26. findDocuments(db,"tbl_setting", {a:2},{ a: 0}, function callback(result){
  27. console.log(result);
  28. });
  29.  
  30. //Update
  31. var new_value = {$set:{text:"Updated by thanhnt."}};
  32. updateDocuments(db, "tbl_setting", {a: 2}, new_value, function callback(result){
  33. //console.log(result);
  34. });
  35.  
  36. //Delete
  37. deleteDocuments(db, "tbl_setting", {a: 1}, function callback(result){
  38. //console.log(result);
  39. });
  40.  
  41. //Aggregate
  42. var lookup =[{
  43. $lookup:{
  44. from: 'documents',
  45. localField: 'a',
  46. foreignField: 'a',
  47. as: "on_document"
  48. }
  49. }];
  50. joinDocuments(db, "tbl_setting", lookup, function callback(result){
  51. console.log(JSON.stringify(result, 2));
  52. });
  53.  
  54. client.close();
  55. });
  56.  
  57. const insertDocuments = function(db, table, data, callback) {
  58. // Get the documents collection
  59. const collection = db.collection(table);
  60. // Insert some documents
  61. collection.insertMany(data, function(err, result) {
  62. assert.equal(err, null);
  63. //assert.equal(3, result.result.n);
  64. //assert.equal(3, result.ops.length);
  65. console.log("Inserted "+ result.result.n +" documents into the collection");
  66. callback(result);
  67. });
  68. }
  69.  
  70. const findDocuments = function(db, table, condition, projection, callback){
  71. const collection = db.collection(table);
  72. // Find documents
  73. collection.find(condition, { "projection": projection}).toArray(function abc(err, docs){
  74. assert.equal(err, null);
  75. console.log("Found the following records");
  76. //console.log(docs);
  77. callback(docs);
  78. });
  79. }
  80.  
  81. const deleteDocuments = function(db, table, condition, callback){
  82. const collection = db.collection(table);
  83. // Delete documents
  84. collection.deleteMany(condition, function(err, result) {
  85. assert.equal(err, null);
  86. //assert.equal(3, result.result.n);
  87. //assert.equal(3, result.ops.length);
  88. console.log("Deleted "+ result.result.n +" documents into the collection");
  89. callback(result);
  90. });
  91. }
  92.  
  93. const updateDocuments = function(db, table, condition, new_value, callback){
  94. const collection = db.collection(table);
  95. // Update documents
  96. collection.updateMany(condition, new_value, function(err, result){
  97. assert.equal(err, null);
  98.  
  99. console.log("Updated "+ result.result.nModified +" documents into the collection");
  100. callback(result);
  101. });
  102. }
  103.  
  104. const joinDocuments = function(db, table, join_field, callback ){
  105. const collection = db.collection(table);
  106. //Join documents
  107. collection.aggregate(join_field).toArray(function abc(err, docs){
  108. assert.equal(err, null);
  109. console.log("Found the following records");
  110. //console.log(docs);
  111. callback(docs);
  112. });
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement