Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Last active August 6, 2022 07:03
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dhavaln/cfc07aa6d1d7144ba6983fd2d3c5713c to your computer and use it in GitHub Desktop.
MongoDB Atlas Serverless with NodeJS 4.1 Drivers
MONGO_URL=mongodb+srv://<USER>:<PASSWORD>@<LB>.mongodb.net/?retryWrites=true&w=majority
DB=
COLLECTION=
require('dotenv').config();
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = process.env.MONGO_URL;
const DB = process.env.DB;
const DB_COLL = process.env.COLLECTION;
const client = new MongoClient(uri,
{
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1
}
);
async function run() {
try {
await client.connect();
const database = client.db(DB);
const testColl = database.collection(DB_COLL);
const query = { };
const options = {};
const cursor = testColl.find(query, options);
// no documents found
if ((await cursor.count()) === 0) {
console.log("No documents found!");
}
await cursor.forEach(console.dir);
} finally {
await client.close();
}
};
run();
{
"name": "mongodb-serverless",
"version": "1.0.0",
"description": "MongoDB Atlas Serverless Testing with NodeJS",
"main": "index.js",
"scripts": {
},
"keywords": [],
"author": "Dhaval Nagar",
"license": "ISC",
"dependencies": {
"dotenv": "^10.0.0",
"mongodb": "^4.1.0",
"reflect-metadata": "^0.1.13",
"resolve-mongodb-srv": "^1.1.0",
"typeorm": "^0.2.34"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment