You can easily connect a LoopBack application to multiple different data sources.

Page Contents

Add a data source

You’re going to add a MongoDB data source in addition to the MySQL data source created in Connect your API to a data source.

$ lb datasource

When prompted, respond as follows:

? Enter the data-source name: mongoDs
? Select the connector for mongoDs: MongoDB (supported by StrongLoop)

Next, the generator will prompt you to type the datasource settings, such as host, port, user, password and database name, as well as to install the database connector.

? Enter the datasource name: mongodb
? Select the connector for mongodb: MongoDB (supported by StrongLoop)
? Connection String url to override other settings (eg: mongodb://username:password@hostname:port/database):
? host: localhost
? port: 27017
? user: demo
? password: ****
? database: demo
? Install loopback-connector-mongodb@^1.4 Yes

If you don’t have the tool install the database connector when it prompts you , then you must install it yourself using npm install. If you don’t enter the database credentials when prompted, then you must add them manually to server/datasources.json as shown below.

Install MongoDB connector

$ npm install --save loopback-connector-mongodb

Configure data source

Edit server/datasources.json to configure the data source so that it connects to the StrongLoop demo MongoDB server.  Add the following JSON after the two existing data source definitions (for “db” and “mysqlDs”):

server/datasources.json

...
"mongoDs": {
  "name": "mongoDs",
  "connector": "mongodb",
  "host": "demo.strongloop.com",
  "port": 27017,
  "database": "getting_started_intermediate",
  "username": "demo",
  "password": "L00pBack"
}

Next: Continue to Create new models.