example.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. 'use strict'
  2. const path = require('path')
  3. const { db, utils } = require('./lib')
  4. const setup = async () => {
  5. // Connect to the database
  6. const connection = await db.connect('mongodb://diran.univ-littoral.fr:27017/webexpe')
  7. console.log('The database connection was established.')
  8. // Find every documents being experiment data
  9. const res = await db.findCustom({
  10. msgId: 'EXPERIMENT_DATA'
  11. })
  12. // Output to console (Do this if you want to pipe to another command!)
  13. console.log(res)
  14. console.log(`Found ${res.length} documents matching your request.`)
  15. // Save the results to a JSON file
  16. const filePath = path.resolve(__dirname, 'searches', `search-${Date.now()}.json`)
  17. // `res` = search results, `filePath` = output file, `true` = JSON will be pretty-printed (human-readable)
  18. await utils.outputToFile(res, filePath, true)
  19. console.log(`Your search results were saved to ${filePath}`)
  20. // Disconnect from the database
  21. await db.disconnect(connection)
  22. console.log('The database connection was destroyed.')
  23. }
  24. setup()