mongo provider

MongoDB Provider for Vapor

40
18
Swift

Mongo

Mongo Provider

Swift
CircleCI
Slack Status

⬇ Install the MongoDB server

OS X

For more instructions, check out https://docs.mongodb.com/master/administration/install-community/.

brew install mongodb

Linux

sudo apt-get update
sudo apt-get install mongodb

Run the server

mongod

📦 Add Provider

Now to use Mongo in your Vapor project.

Package

Add the package to your Package.swift.

.Package(url: "https://github.com/vapor/mongo-provider.git", majorVersion: 2)

Droplet

Add the provider to your Droplet.

import Vapor
import MongoProvider

let drop = Droplet()
try drop.addProvider(MongoProvider.Provider.self)

And configure Fluent to use MongoDB by changing the key "driver" to be "mongo" inside `Config/fluent.json

Config

Then add a mongo.json to your Config folder. You can add it in the root or keep it out of git in the secrets folder.

Config/
  - mongo.json
    secrets/
      - mongo.json

The secrets folder is under the .gitignore and shouldn’t be committed.

Here’s an example secrets/mongo.json

{
  "url": "mongodb://<db-user>:<db-password>@<host>:<port>/<database>"
}

Note: port and host are optional.

Manual

You can also manually configure the provider in code. This will bypass the configuration files.

import Vapor
import MongoProvider

let drop = Droplet()

let mongo = try MongoProvider.Provider(database: ..., user: ..., password: ...)
drop.addProvider(mongo)

📖 Documentation

Visit the Vapor web framework’s documentation for more instructions on how to use this package.

💧 Community

Join the welcoming community of fellow Vapor developers in slack.

🔧 Compatibility

This package has been tested on macOS and Ubuntu.