ortense
10/6/2017 - 2:46 PM

mongodb-with-bluebird.js

const { MongoClient } = require('mongodb')
const Bluebird = require('bluebird')

const DBURL = 'mongodb://localhost/demo'
const COLNAME = 'demo'

MongoClient.connect(DBURL, { promiseLibrary: Bluebird })
  .tap(() => console.log('Connected!'))
  .then(async db => {
    const result = await db.collection(COLNAME).insert({ value: Math.random() })
    return result.ops
  })
  .map(({ _id, value }) => ({
    id: _id.toString(),
    value,
  }))
  .then(console.log)
  .catch(console.log)