Skip to content

Instantly share code, notes, and snippets.

@lineus lineus/output.txt Secret
Created Aug 17, 2018

Embed
What would you like to do?
using native driver with srv and options in object
$: ./srvUriNoCreds.js
version: 3.1.3
conn string: mongodb+srv://cluster.mongodb.net/?retryWrites=true
{ _id: 5b7718923eace33f25684a05, name: 'Billy' }
$:
#!/usr/bin/env node --no-deprecation
'use strict';
const env = require('/Users/lineus/.env');
const { MongoClient } = require('mongodb');
const uri = env.ATLASSRV_HOST_ONLY;
const opts = { auth: { user: env.ATLAS_USER, password: env.ATLAS_PASS } };
const sharable = uri.replace( /cl[^:]+mon/, 'cluster.mon');
const pkg = require.resolve('mongodb').replace(/\/[^/]*$/, '/package.json');
const ver = require(pkg).version;
async function run() {
console.log(`version: ${ver}`);
console.log(`conn string: ${sharable}`);
const client = await MongoClient.connect(uri, opts);
const db = client.db('gh-6667');
await db.dropDatabase().catch(handleError);
await db
.collection('tests')
.insert({ name: 'Billy' })
.catch(handleError);
let found = await db
.collection('tests')
.findOne({})
.catch(handleError);
console.log(found);
process.exit(0);
}
run();
function handleError(e) {
return console.error(e.message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.