Skip to content

Instantly share code, notes, and snippets.

@bengrunfeld
Created August 12, 2020 07:26
Show Gist options
  • Save bengrunfeld/48eecad69f0d2dc9c08ce8f2c4f55676 to your computer and use it in GitHub Desktop.
Save bengrunfeld/48eecad69f0d2dc9c08ce8f2c4f55676 to your computer and use it in GitHub Desktop.
Use Node.JS to read all records from a MongoDB Collection
const readAllRecords = db => {
const p = new Promise((resolve, reject) => {
db.collection(process.env.COL_NAME)
.find({})
.toArray((error, results) => {
if (error) {
reject({ origin: "readAllRecords", error });
return;
}
resolve(results);
});
});
p.catch(error => console.log("Error in readAllRecords:", error));
return p;
};
export default readAllRecords;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment