SHARE
TWEET

Untitled

a guest May 23rd, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. var MongoClient = require('mongodb').MongoClient;
  4.  
  5. MongoClient.connect( 'mongodb://127.0.0.1:27017/accounting', function (err, connection) {
  6.  
  7. var collection = connection.collection('customers');
  8.  
  9. var doFind = function (callback) { collection.find().toArray(function (err, documents) {
  10.         console.dir(documents);
  11.         callback();
  12.       });
  13.  
  14. };
  15. var doInsert = function (i) { if (i < 20) {
  16. var value = Math.floor(Math.random() * 10); collection.insert(
  17. {'n': '#' + i, 'v': value}, function (err, count) {
  18.         doInsert(i + 1);
  19.       });
  20. } else {
  21. console.log();
  22. console.log('Inserted', i, 'documents:'); doFind(function () {
  23.       doUpdate();
  24.     });
  25. } };
  26. var doUpdate = function () {
  27.  
  28. collection.update(
  29.  
  30. {'v': {'$gt': 5}},
  31. {'$set': {'valuable': true}}, {'multi': true},
  32.  
  33. function (err, count) {
  34.  
  35. console.log();
  36. console.log('Updated', count, 'documents:');
  37.  
  38. doFind(function () {
  39. collection.remove({}, function () { connection.close();
  40. });
  41. });
  42. });
  43. };
  44.     doInsert(0);
  45.   });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top