$ npm install --save mongoose feathers-mongoose
This adapter also requires a running MongoDB database server.
Returns a new service instance initialized with the given options.
Model has to be a Mongoose model. See the Mongoose Guide for more information on defining your model.
const mongoose = ;const service = ;// A module that exports your Mongoose modelconst Model = ;// Make Mongoose use the ES6 promisemongoosePromise = globalPromise;// Connect to a local database called `feathers`mongoose;app;app;
Model(required) - The Mongoose model definition
true) - Runs queries faster by returning plain objects instead of Mongoose models.
'_id') - The name of the id field property.
events(optional) - A list of custom service events sent by this service
paginate(optional) - A pagination object containing a
discriminators(optional) - A list of mongoose models that inherit from
Important: To avoid odd error handling behaviour, always set
mongoose.Promise = global.Promise. If not available already, Feathers comes with a polyfill for native Promises.
Important: When setting
mongoose property is also useful for performing upserts on a
patch request. "Upserts" do an update if a matching record is found, or insert a record if there's no existing match. The following example will create a document that matches the
data, or if there's already a record that matches the
params.query, that record will be updated.
const data = address: '123' identifier: 'my-identifier'const params =query: address: '123'mongoose: upsert: trueapp
Here's a complete example of a Feathers server with a
messages Mongoose service.
$ npm install @feathersjs/feathers @feathersjs/errors @feathersjs/express mongoose feathers-mongoose
const mongoose = ;const Schema = mongooseSchema;const MessageSchema =text:type: Stringrequired: true;const Model = mongoose;moduleexports = Model;
const feathers = ;const express = ;const socketio = ;const mongoose = ;const service = ;const Model = ;mongoosePromise = globalPromise;// Connect to your MongoDB instance(s)mongoose;// Create an Express compatible Feathers application instance.const app = ;// Turn on JSON parser for REST servicesapp;// Turn on URL-encoded parser for REST servicesapp;// Enable REST servicesapp;// Enable Socket.io servicesapp;// Connect to the db, create and register a Feathers service.app;app;// Create a dummy Messageapp;// Start the server.const port = 3030;app;
You can run this example by using
node app and go to localhost:3030/messages.
Mongoose by default gives you the ability to add validations at the model level. Using an error handler like the one that comes with Feathers your validation errors will be formatted nicely right out of the box!
For more information on querying and validation refer to the Mongoose documentation.
For Mongoose, the special
$populate query parameter can be used to allow Mongoose query population.
Instead of strict inheritance, Mongoose uses discriminators as their schema inheritance model.
To use them, pass in a
discriminatorKey option to your schema object and use
Model.discriminator('modelName', schema) instead of
Feathers comes with full support for mongoose discriminators, allowing for automatic fetching of inherited types. A typical use case might look like:
var mongoose = ;var Schema = mongooseSchema;var Post = ;var feathers = ;var app = ;var service = ;// Discriminator key, we'll use this later to refer to all text postsvar options =discriminatorKey: '_type';var TextPostSchema =text: type: String default: nulloptions;// Note the use of `Post.discriminator` rather than `mongoose.discriminator`.var TextPost = Post;// Using the discriminators option, let feathers know about any inherited models you may have// for that serviceapp
Without support for discriminators, when you perform a
.get on the posts service, you'd only get back
Post models, not
Now in your query, you can specify a value for your discriminatorKey:
and Feathers will automatically swap in the correct model and execute the query it instead of its parent model.