Edit Page

Using MongoDB with Node.js/Sails.js

Sails supports the popular MongoDB database via the sails-mongo adapter.

First, make sure you have access to a running MongoDB server, either on your development machine or in the cloud. Below, 'mongodb://[email protected]/foo' refers to a locally-installed MongoDB using "foo" as the database name. Be sure to replace that connection URL with the appropriate string for your database.

Developing locally with MongoDB

To use MongoDB in your Node.js/Sails app during development:

  1. Run npm install sails-mongo in your app folder.
  2. In your config/datastores.js file, edit the default datastore configuration:

    default: {
       adapter: 'sails-mongo',
       url: 'mongodb://[email protected]/foo'
     }
    
  3. In your config/models.js file, edit the default id attribute to have the appropriate type and columnName for MongoDB's primary keys:

    attributes: {
       id: { type: 'string', columnName: '_id' },
       //…
     }
    

That's it! Lift your app again and you should be good to go.

Case-sensitivity

After configuring your project to use MongoDB, you may notice that your Waterline queries are now case-sensitive by default. To do case-insensitive queries, you can use .meta({makeLikeModifierCaseInsensitive: true}).

Deploying your app with MongoDB

To use MongoDB in production, edit your adapter setting in config/env/production.js:

adapter: 'sails-mongo',

You may also configure your connection URL -- but many developers prefer not to check sensitive credentials into version control. Another option is to use an environment variable:

sails_datastores__default__url=mongodb://heroku_12345678:[email protected]:29017/heroku_12345678

To use MongoDB in your staging environment, edit config/env/staging.js. Depending on your application, it may be acceptable to check in your staging database credentials to version control, since they are less of a security risk.

Low-level MongoDB usage (advanced)

As with all of the Sails database adapters, you can use any of the Waterline model methods to interact with your models when using sails-mongo.

For many apps, that's all you'll need-- from "hello world" to production. Even if you run into limitations, they can usually be worked around without writing Mongo-specific code. However, for situations when there is no alternative, it is possible to use the Mongo driver directly in your Sails app.

To access the lower-level “native” MongoDB client directly, use the .manager property of the datastore instance:

// Get access to the native MongoDB client via the default Sails datastore.
var db = sails.getDatastore().manager;

// Find all users who own albums with the word "blue" in the title.
// ("albums" would be defined in `api/models/User.js` as an attribute of type "json".)
db.collection('user').find({"albums.title": {"$regex": /blue/}}).toArray(console.log);

For a full list of methods available in the native MongoDB client, see the Node.js MongoDB Driver API reference.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Sails logo
  • Home
  • Get started
  • Support
  • Documentation
  • Documentation

For a better experience on sailsjs.com, update your browser.

Documentation

Reference Concepts App structure | Upgrading Contribution guide | Tutorials More

Tutorials

  • Full-stack JavaScript with Sails
  • Low-level MySQL usage (advanced)
  • Using CoffeeScript
  • Using MongoDB
  • Using TypeScript

Built with Love

The Sails framework is built by a web & mobile shop in Austin, TX, with the help of our contributors. We created Sails in 2012 to assist us on Node.js projects. Naturally we open-sourced it. We hope it makes your life a little bit easier!

Sails:
  • What is Sails?
  • Community
  • News
  • For business
About:
  • Our company
  • Security
  • Legal
  • Logos/artwork
Help:
  • Get started
  • Documentation
  • Docs
  • Contribute
  • Take a class

© 2012-2021 The Sails Company. 
The Sails framework is free and open-source under the MIT License. 
Illustrations by Edamame.