Skip to content

Instantly share code, notes, and snippets.

@Saad-ISAA
Last active May 14, 2022 16:31
Show Gist options
  • Save Saad-ISAA/8676429210a7f321280ebde62322a8b8 to your computer and use it in GitHub Desktop.
Save Saad-ISAA/8676429210a7f321280ebde62322a8b8 to your computer and use it in GitHub Desktop.
// this is a helper utility class to help with mongodb queries and operations
// function to connect to mongodb and disconnect
const MongoClient = require('mongodb').MongoClient;
// change connect close to as required
// connect to mongodb on every call and connection details from env variables
const connect = async () => {
const client = await MongoClient.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const mongodb = client.db(process.env.MONGO_DB);
return mongodb;
};
// function to create entry in mongodb and return whole object with id
const insertOne = async (mongodb, collection, data) => {
const result = await mongodb.collection(collection).insertOne(data);
return result.insertedId;
};
// function to search entry in mongodb with filteration and pagination with default values to return all pages
const find = async (mongodb, collection, filter, page = 1, limit = 10) => {
const result = await mongodb.collection(collection).find(filter).skip((page - 1) * limit).limit(limit).toArray();
return result;
};
// function to search entry in mongodb with filteration without pagination
const findOne = async (mongodb, collection, filter) => {
const result = await mongodb.collection(collection).findOne(filter);
return result;
};
// function to fetch all entries in a collection
const findAll = async (mongodb, collection) => {
const result = await mongodb.collection(collection).find().toArray();
return result;
};
// function to find and update an entry
const findAndUpdate = async (mongodb, collection, filter, data) => {
const result = await mongodb.collection(collection).findOneAndUpdate(filter, { $set: data }, { returnOriginal: false });
return result.value;
};
// function for findOneAndDelete
const findOneAndDelete = async (mongodb, collection, filter) => {
const result = await mongodb.collection(collection).findOneAndDelete(filter);
return result.value;
};
// function for findOneAndUpdate
const findOneAndUpdate = async (mongodb, collection, filter, data) => {
const result = await mongodb.collection(collection).findOneAndUpdate(filter, { $set: data }, { returnOriginal: false });
return result.value;
};
// function to update an entry in mongodb with filteration
const update = async (mongodb, collection, filter, data) => {
const result = await mongodb.collection(collection).updateOne(filter, { $set: data });
return result.modifiedCount;
};
// function to update all entries if condition matches in a collection
const updateAll = async (mongodb, collection, filter, data) => {
const result = await mongodb.collection(collection).updateMany(filter, { $set: data });
return result.modifiedCount;
};
// function for deleteOne in mongodb
const deleteOne = async (mongodb, collection, filter) => {
const result = await mongodb.collection(collection).deleteOne(filter);
return result.deletedCount;
};
// function for deleteMany in mongodb
const deleteMany = async (mongodb, collection, filter) => {
const result = await mongodb.collection(collection).deleteMany(filter);
return result.deletedCount;
};
// function to count the number of entries in a collection
const count = async (mongodb, collection, filter) => {
const result = await mongodb.collection(collection).countDocuments(filter);
return result;
};
// function to clount all entries in a collection
const countAll = async (mongodb, collection) => {
const result = await mongodb.collection(collection).countDocuments();
return result;
};
// function to create a collection in db
const createCollection = async (mongodb, collection) => {
const result = await mongodb.createCollection(collection);
return result;
}
// function to list all collections in db
const listCollections = async (mongodb,) => {
const result = await mongodb.listCollections().toArray();
return result;
}
// function to drop a collection in db
const dropCollection = async (mongodb, collection) => {
const result = await mongodb.collection(collection).drop();
return result;
};
module.exports = {
connect,
insertOne,
find,
findOne,
findAll,
findAndUpdate,
findOneAndDelete,
findOneAndUpdate,
update,
updateAll,
deleteOne,
deleteMany,
count,
countAll,
createCollection,
listCollections,
dropCollection,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment