A simplified wrapper layer above the node-mongodb-native driver that provides a streamlined API for MongoDB database operations
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Enhanced collection interface with convenience methods for common database operations, including findById, updateById, and removeById.
Standard MongoDB collection operations with enhanced connection handling.
/**
* Insert documents into collection
* @param {object|object[]} docs - Document(s) to insert
* @param {object} [options] - Insert options
* @param {function} [callback] - Callback function (err, result)
* @returns {SkinCollection} this
*/
insert(docs, options, callback);
/**
* Update documents in collection
* @param {object} query - Query selector
* @param {object} update - Update operations
* @param {object} [options] - Update options
* @param {function} [callback] - Callback function (err, result)
* @returns {SkinCollection} this
*/
update(query, update, options, callback);
/**
* Remove documents from collection
* @param {object} query - Query selector
* @param {object} [options] - Remove options
* @param {function} [callback] - Callback function (err, result)
* @returns {SkinCollection} this
*/
remove(query, options, callback);
/**
* Find a single document
* @param {object} query - Query selector
* @param {object} [options] - Query options
* @param {function} [callback] - Callback function (err, doc)
* @returns {SkinCollection} this
*/
findOne(query, options, callback);Mongoskin provides several enhanced find methods for common query patterns.
/**
* Find documents and return cursor or call callback
* @param {object} [query] - Query selector
* @param {object} [options] - Query options
* @param {function} [callback] - Callback function (err, cursor)
* @returns {SkinCursor|SkinCollection} cursor if no callback, otherwise this
*/
find(query, options, callback);
/**
* Find documents and return array directly
* @param {object} [query] - Query selector
* @param {object} [options] - Query options
* @param {function} callback - Callback function (err, items)
* @returns {SkinCollection} this
*/
findItems(query, options, callback);
/**
* Find documents and iterate with each callback
* @param {object} [query] - Query selector
* @param {object} [options] - Query options
* @param {function} eachCallback - Callback for each document (err, item)
* @returns {SkinCollection} this
*/
findEach(query, options, eachCallback);Usage Examples:
const mongoskin = require('mongoskin');
const db = mongoskin.db('mongodb://localhost:27017/myapp');
const users = db.collection('users');
// Standard find with cursor
users.find({ active: true }, (err, cursor) => {
if (err) throw err;
cursor.toArray((err, docs) => {
console.log('Found users:', docs);
});
});
// Find and get array directly
users.findItems({ role: 'admin' }, (err, admins) => {
if (err) throw err;
console.log('Admin users:', admins);
});
// Find and iterate each document
users.findEach({ age: { $gt: 18 } }, (err, user) => {
if (err) throw err;
if (user) {
console.log('Adult user:', user.name);
} else {
console.log('No more users');
}
});Convenient methods for operations using document _id.
/**
* Find document by _id
* @param {string|ObjectID|number} id - Document _id
* @param {function} callback - Callback function (err, doc)
* @returns {SkinCollection} this
*/
findById(id, callback);
/**
* Update document by _id
* @param {string|ObjectID|number} id - Document _id
* @param {object} doc - Update operations
* @param {function} [callback] - Callback function (err, result)
* @returns {SkinCollection} this
*/
updateById(id, doc, callback);
/**
* Remove document by _id
* @param {string|ObjectID|number} id - Document _id
* @param {function} [callback] - Callback function (err, result)
* @returns {SkinCollection} this
*/
removeById(id, callback);Usage Examples:
const mongoskin = require('mongoskin');
const db = mongoskin.db('mongodb://localhost:27017/myapp');
const users = db.collection('users');
// Find by ID (string, ObjectID, or number)
users.findById('507f1f77bcf86cd799439011', (err, user) => {
if (err) throw err;
console.log('Found user:', user);
});
// Update by ID
users.updateById('507f1f77bcf86cd799439011', { $set: { active: false } }, (err, result) => {
if (err) throw err;
console.log('Update result:', result);
});
// Remove by ID
users.removeById('507f1f77bcf86cd799439011', (err, result) => {
if (err) throw err;
console.log('Remove result:', result);
});Bind custom methods to collection instances for reusable functionality.
/**
* Bind custom methods or properties to collection
* @param {object} extendObject - Object with methods/properties to bind
* @returns {void}
*/
bind(extendObject);Usage Examples:
const mongoskin = require('mongoskin');
const db = mongoskin.db('mongodb://localhost:27017/myapp');
const users = db.collection('users');
// Bind custom methods
users.bind({
findByEmail: function(email, callback) {
this.findOne({ email: email }, callback);
},
findActiveUsers: function(callback) {
this.findItems({ active: true }, callback);
},
getUserStats: function(callback) {
this.aggregate([
{ $group: { _id: '$role', count: { $sum: 1 } } }
], callback);
}
});
// Use custom methods
users.findByEmail('user@example.com', (err, user) => {
if (err) throw err;
console.log('User by email:', user);
});
users.findActiveUsers((err, activeUsers) => {
if (err) throw err;
console.log('Active users:', activeUsers.length);
});Collection index operations for performance optimization.
/**
* Ensure index exists on collection
* @param {object} fieldOrSpec - Index specification
* @param {object} [options] - Index options
* @param {function} [callback] - Callback function (err, indexName)
* @returns {SkinCollection} this
*/
ensureIndex(fieldOrSpec, options, callback);
/**
* Create index on collection
* @param {object} fieldOrSpec - Index specification
* @param {object} [options] - Index options
* @param {function} [callback] - Callback function (err, indexName)
* @returns {SkinCollection} this
*/
createIndex(fieldOrSpec, options, callback);
/**
* Drop index from collection
* @param {string} indexName - Name of index to drop
* @param {function} [callback] - Callback function (err, result)
* @returns {SkinCollection} this
*/
dropIndex(indexName, callback);
/**
* List all indexes on collection
* @param {function} callback - Callback function (err, indexes)
* @returns {SkinCollection} this
*/
indexes(callback);Usage Examples:
const users = db.collection('users');
// Create single field index
users.ensureIndex({ email: 1 }, { unique: true }, (err, indexName) => {
if (err) throw err;
console.log('Created index:', indexName);
});
// Create compound index
users.createIndex({ name: 1, age: -1 }, (err, indexName) => {
if (err) throw err;
console.log('Created compound index:', indexName);
});
// List all indexes
users.indexes((err, indexes) => {
if (err) throw err;
console.log('Collection indexes:', indexes);
});MongoDB aggregation pipeline operations.
/**
* Perform aggregation operation
* @param {object[]} pipeline - Aggregation pipeline stages
* @param {object} [options] - Aggregation options
* @param {function} [callback] - Callback function (err, result)
* @returns {SkinCollection} this
*/
aggregate(pipeline, options, callback);
/**
* Perform map-reduce operation
* @param {function} map - Map function
* @param {function} reduce - Reduce function
* @param {object} [options] - MapReduce options
* @param {function} [callback] - Callback function (err, result)
* @returns {SkinCollection} this
*/
mapReduce(map, reduce, options, callback);Usage Examples:
const users = db.collection('users');
// Aggregation pipeline
users.aggregate([
{ $match: { active: true } },
{ $group: { _id: '$department', count: { $sum: 1 } } },
{ $sort: { count: -1 } }
], (err, result) => {
if (err) throw err;
console.log('Department stats:', result);
});
// Map-reduce operation
const map = function() { emit(this.department, 1); };
const reduce = function(key, values) { return values.length; };
users.mapReduce(map, reduce, { out: { inline: 1 } }, (err, result) => {
if (err) throw err;
console.log('Map-reduce result:', result);
});