Registry / database / egg-mongo-native

egg-mongo-native

JSON →
library3.5.0jsnpmunverified

Egg.js plugin providing MongoDB integration via the official node-mongodb-native driver. Version 3.5.0 (stable). Wraps common MongoDB operations (find, insertOne, updateOne, deleteOne, aggregate) into convenient Egg.js patterns with collection name and args object, while exposing the full native driver API. Requires Egg.js application framework. Supports single instance, replica sets (v2.1.0+), and multiple instances via clients configuration. Low release cadence, focused on stability.

npm install egg-mongo-native
INSTALL
IMPORT
SIG · EGG-MONGO-NATIVE
E
egg-mongo-native
databasejavascriptv3.5.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

plugin configuration (config/plugin.js)
// {app_root}/config/plugin.js exports.mongo = { enable: true, package: 'egg-mongo-native', };
// Incorrect: using require in plugin config exports.mongo = require('egg-mongo-native');
Plugin must be declared in config/plugin.js with enable and package properties, not imported.
app.mongo
// After enabling plugin, access via app.mongo await app.mongo.find('collection', { query: {}, limit: 10 });
// Wrong: direct require const mongo = require('egg-mongo-native'); await mongo.find(...);
Plugin instances are injected into app by Egg.js. Do not use require in controller/service files.
multiple databases (app.mongo.get)
// When using clients configuration const db1 = app.mongo.get('db1'); await db1.find('collection', {});
// Mist: using app.mongo directly for queries await app.mongo.find('collection', {}); // This fails with multiple instances
With clients config, use app.mongo.get(name) to obtain the specific instance before calling methods.

Complete setup: install, enable plugin, configure single MongoDB instance, and use in an Egg.js controller to query a collection.

// 1. Install: npm i egg-mongo-native --save // 2. Enable in {app_root}/config/plugin.js: exports.mongo = { enable: true, package: 'egg-mongo-native', }; // 3. Configure in {app_root}/config/config.default.js: exports.mongo = { client: { host: process.env.MONGO_HOST || 'localhost', port: process.env.MONGO_PORT || '27017', name: process.env.MONGO_DB || 'test', user: process.env.MONGO_USER || '', password: process.env.MONGO_PASSWORD || '', options: {}, }, }; // 4. Use in controller: 'use strict'; const Controller = require('egg').Controller; class HomeController extends Controller { async index() { const ctx = this.ctx; // Find documents const result = await ctx.app.mongo.find('users', { query: { name: 'Alice' }, limit: 10, }); ctx.body = result; } } module.exports = HomeController;
Debug
Known issues
breakingv3.0.0 changed method signatures - now requires collection name as first arg and args object as second.
fix
Update calls from app.mongo.find('collection', query, callback) to app.mongo.find('collection', { query, ... }).
affects: >=3.0.0
breakingReplica Set support requires v2.1.0 or higher. Earlier versions do not support comma-separated hosts/ports.
fix
Upgrade to v2.1.0+ for replica set configuration.
affects: <2.1.0
gotchaCannot set both 'client' and 'clients' in config. They are mutually exclusive.
fix
Use only one: either 'client' for single instance or 'clients' for multiple databases.
affects: >0.0.0
deprecatedOld callback style (without promises or async/await) is deprecated in v3+. Use Promises or async/await.
fix
Convert callbacks to async/await: const result = await app.mongo.find(...);
affects: >=3.0.0 <4.0.0
gotchaMethod names differ from native driver. For example, app.mongo.insertOne maps to db.collection.insertOne.
fix
Check the plugin README for the full list of mapped methods.
affects: >0.0.0
Errors
Common errors & fixes
Error: Cannot find module 'egg-mongo-native'
Package not installed or plugin not enabled properly.
fix
Run 'npm i egg-mongo-native --save' and ensure it is listed in package.json. Also check config/plugin.js for correct enable and package fields.
TypeError: app.mongo.find is not a function
Using multiple instances ('clients' config) but calling methods directly on app.mongo.
fix
Use app.mongo.get('dbName') to get the specific database instance first, then call methods on that instance.
MongoError: Authentication failed
Incorrect MongoDB credentials in config.
fix
Check config.default.js for correct user, password, and authSource (defaults to 'admin'). Alternatively, use MONGO_URI environment variable.
Upgrade
Version history
3.5.0latest on npm
Audit
Dependencies
eggrequiredEgg.js framework required for plugin lifecycle
Agent activity
7 hits · last 30 days
node
6
Resources
egg-mongo-native — npm install egg-mongo-native · libregistry