Pnk-Node-Mongo
A reusable and lightweight Node.js module that helps you easily integrate Mongoose-based MongoDB queries with pagination, filtering, sorting, searching, field selection, and update utilities.
🚀 Installation
npm install pnk-node-mongo
You can use this Library for below operations in Node with MongoDB
- findOne
- find with Pagination
- updateMany
- updateOne
- findOneAndUpdate etc
- insert one and many
📦 Usage
const { PnkAddNew, PnkFetchAll, PnkFetchOne, PnkUpdate, PnkFindAndModify, PnkResult, PnkResultSummary } = require('pnk-node-mongo');
Fetch Example
//Example with find (use with Post always for Search and Filters)
const { PnkFetchAll } = require('pnk-node-mongo');
GetAllUsers = async (req, res) => {
try {
const options = {
page: req.body.set_no || 1,
per_set: req.body.per_set || 10,
search_data: req.body.search ? { user_name: req.body.user_name } : {},
sorting: { createdAt: -1 },
// specific_data: ['user_name', 'user_email'],
// data_except_few: ['user_password'],
};
const result = await PnkFetchAll("userlistresponse", "documentName in mongodb", options);
console.log(result);
res.status(200).json(result);
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
};
You can update parameters as per use cases.
Find and Modify Example
// Example with findAndModify (use with Post/Put)
const { PnkFindAndModify } = require('pnk-node-mongo');
FindAndModifyUser = async (req, res) => {
try {
const result = await PnkFindAndModify({
respKey: 'userupdateresponse',
model: "documentName in mongodb",
filter: { user_mobile: "0000000000" },
update: { user_name: 'prabhash' },
options: { _id: -1 },
new_data: true,
onlyFields: "user_name, user_email", // <-- return only selected fields
// exceptFields: "user_password, user_origin" // <-- or exclude this field instead
resp: false
})
console.log(result);
res.status(200).json({ success: true, result });
} catch (err) {
res.status(400).json({ success: false, message: err.message });
}
};
* If you need updated data in response then set new_data = true
* If you need data in json format then send resp = true
Update Example
// Example with updateOne and updateMany (use with Post/Put)
const { PnkUpdate } = require('pnk-node-mongo');
exports.UpdateUser = async (req, res) => {
try {
const result = await PnkUpdate({
respKey: 'userupdateresponse',
model: 'documentName in mongodb',
filter: { user_mobile: "7795834234" },
updateData: { user_name: 'prabhash' },
multi: false,
options: { new: false }
});
res.status(200).json({ success: true, result });
} catch (err) {
res.status(400).json({ success: false, message: err.message });
}
};
* If you need multi update then sent true otherwise false
* If you need data to be upserted (insert new) if not found then sent options: { new: true }
Insert Example
// Example with Insert one and Many (use with Post/Put)
const { PnkAddNew } = require('pnk-node-mongo');
exports.UpdateUser = async (req, res) => {
try {
const user_data = {
user_name: 'test',
user_full_name: 'prabhash',
user_email: 'dummy@gmail.com'
user_status: 1,
};
const result = await PnkAddNew(
{ respKey: "userentryresponse",
model: 'documentName in mongodb',
data: user_data,
multi: false,
resp: true });
res.status(200).json({ success: true, result });
} catch (err) {
res.status(400).json({ success: false, message: err.message });
}
};
* If you need multi then data should be in Array only and multi : true
✨ Features
- Paginated fetch with search, filter, and sort support
- Select specific fields or exclude them
- Flexible update operation (single/multi)
- Auto response formatting (optional)
🧪 Testing
npm test
📜 License
MIT