vapor-docs/3.0/docs/databases/mongodb/getting-started.md

3.9 KiB

Using MongoDB

The vapor/mongodb package is an extremely performant, reactive and pure swift MonogDB driver. It provides a simple interface to MongoDB for powerful features.

On top of vapor/mysql, we have built vapor/fluent-sqlite which allows SQLite databases to be used with Fluent.

Just MongoDB

This package works really well with and without Fluent. To include this package in your project, simply add it to your Package manifest.

// swift-tools-version:4.0
import PackageDescription

let package = Package(
    name: "Project",
    dependencies: [
        ...
        .package(url: "https://github.com/OpenKitten/MongoKitten.git", .revision("master/5.0")),
    ],
    targets: [
      .target(name: "Project", dependencies: ["MongoKitten", ... ])
    ]
)

If this is your first time adding a dependency, you should read our introduction to Package.swift.

If this is your first time using MongoDB, have a look at the setup guides.

The official documentation assumed either the MongoDB shell or one of their more popular (official) drivers. Please refer to the MongoKitten interpretation guide before reading the official documentation.

Use import MongoKitten to access the APIs.

With Fluent

To use MongoDB with Fluent, you just need to make sure to add the vapor/fluent-mongodb package to your project.

To do this, add the Fluent MySQL package to your Package manifest.

// swift-tools-version:4.0
import PackageDescription

let package = Package(
    name: "Project",
    dependencies: [
        ...
        .package(url: "https://github.com/vapor/fluent-mongodb.git", .revision("beta")),
    ],
    targets: [
      .target(name: "Project", dependencies: ["FluentMongoDB", ... ])
    ]
)

If this is your first time adding a dependency, you should read our introduction to Package.swift.

Use import FluentMongoDB to access MongoDB's Fluent compatible APIs. Learn more about the Fluent APIs here

More info on working with Fluent can be found here.