MongoDB Native

feathers-mongodb is a database adapter for MongoDB. Unlike Mongoose this is does not have an ORM. You deal with the database directly.

$ npm install --save mongodb feathers-mongodb

Getting Started

The following example will create a messages endpoint and connect to a local messages collection on the feathers database.

var MongoClient = require('mongodb').MongoClient;
var service = require('feathers-mongodb');
var app = feathers();

  app.use('/messages', service({
    Model: db.collection('messages')



The following options can be passed when creating a new MongoDB service:

  • Model (required) - The MongoDB collection instance
  • id (default: '_id') [optional] - The id field for your documents for this service.
  • paginate [optional] - A pagination object containing a default and max page size (see the Pagination chapter)

Complete Example

To run the complete MongoDB example we need to install

$ npm install feathers feathers-rest feathers-socketio feathers-mongodb feathers-errors mongodb body-parser

Then add the following into app.js:

const feathers = require('feathers');
const rest = require('feathers-rest');
const socketio = require('feathers-socketio');
const errors = require('feathers-errors');
const bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
const service = require('feathers-mongodb');

// Create a feathers instance.
const app = feathers()
  // Enable
  // Enable REST services
  // Turn on JSON parser for REST services
  // Turn on URL-encoded parser for REST services
  .use(bodyParser.urlencoded({extended: true}));

  // Connect to your MongoDB instance(s)
  // Connect to the db, create and register a Feathers service.
  app.use('/messages', service({
    Model: db.collection('messages'),
    paginate: {
      default: 2,
      max: 4

  // A basic error handler, just like Express

  // Start the server
  var server = app.listen(3030);
  server.on('listening', function() {
    console.log('Feathers Message MongoDB service running on');

You can run this example from the GitHub repository with npm start and going to localhost:3030/messages. You should see an empty array. That's because you don't have any messages yet but you now have full CRUD for your new messages service.

results matching ""

    No results matching ""