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
Important:
If you have a MongoDB database server that you can use, please do so. Create a new database called “getting_started_intermediate”. If you wish, you can use a different database name. Just make sure the mongoDs.database
property in server/datasources.json
matches it (see below).
If not, you can use the StrongLoop MongoDB server running on demo.strongloop.com. However, be aware that it is a shared resource. There is a small chance that two users may perform operations that conflict. For this reason, we recommend you use your own MongoDB server if you have one.
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”):
...
"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.