brew install mongosudo apt-get update
sudo apt-get install mongomongodNow to use Mongo in your Vapor project.
Add the package to your Package.swift.
.Package(url: "https://github.com/vapor/mongo-provider.git", majorVersion: 1, minor: 1)Add the provider to your Droplet.
import Vapor
import VaporMongo
let drop = Droplet()
try drop.addProvider(VaporMongo.Provider.self)Note: If on Xcode you get a No such module ‘VaporMongo’ error, do a
vapor xcodecommand. If that still doesn’t work do avapor clean && vapor xcodecommand.
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
{
"user": "username",
"password": "badpassword",
"database": "databasename",
"port": "27017",
"host": "z99a0.asdf8c8cx.us-east-1.rds.amazonaws.com"
}Note:
portandhostare optional.
You can also manually configure the provider in code. This will bypass the configuration files.
import Vapor
import VaporMongo
let drop = Droplet()
let mongo = try VaporMongo.Provider(database: ..., user: ..., password: ...)
drop.addProvider(mongo)Visit the Vapor web framework's documentation for more instructions on how to use this package.
Join the welcoming community of fellow Vapor developers in slack.
This package has been tested on macOS and Ubuntu.