vg-mongo is a MongoDB client, based on the mongodb package, with pagination support. It offers a simplified interface for interacting with MongoDB, facilitating the implementation of common database functions.
- Based on the official MongoDB package for Node.js.
- Integrated support for pagination.
- Facilitates access to collections and database operations.
To install vg-mongo, use npm:
npm install vg-mongo
To start and use vg-mongo:
const vgMongo = require('vg-mongo');
// Database connection
const db = await vgMongo('mongodb://localhost:27017', 'myDatabase', { useNewUrlParser: true, useUnifiedTopology: true });
// Using a collection
const myCollection = db.myCollection;
// Closing the connection
await db.close();
The vg-mongo library includes an incorporated pagination function:
// Using pagination in a collection
const query = {}; // Your MongoDB query here
const fields = {}; // Specific fields you want to retrieve
const options = {
limit: 10, // Number of documents per page
page: 1, // Current page number
sort: { _id: 1 } // Sort by _id in ascending order
};
// Get the paginated documents
const paginatedResults = await myCollection.paginate(query, fields, options);
console.log(paginatedResults);
query
: Search criteria in MongoDB.fields
: Fields to return in the documents.options
: Pagination parameters such aslimit
,page
, andsort
.
To run tests:
npm test
Contributions are welcome. Please check the open issues for reporting bugs or suggesting improvements.
vg-mongo is under the ISC license.