-
-
Save dhavaln/cfc07aa6d1d7144ba6983fd2d3c5713c to your computer and use it in GitHub Desktop.
MongoDB Atlas Serverless with NodeJS 4.1 Drivers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MONGO_URL=mongodb+srv://<USER>:<PASSWORD>@<LB>.mongodb.net/?retryWrites=true&w=majority | |
DB= | |
COLLECTION= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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