more mongodb docs

This commit is contained in:
Joannis Orlandos 2018-01-02 14:18:42 +01:00
parent 9a8b7ed417
commit 0609151887
2 changed files with 32 additions and 18 deletions

View File

@ -35,7 +35,7 @@ Use `import MongoKitten` to access the APIs.
## With Fluent
MySQL works well Fluent, you just need to make sure to add the [vapor/fluent-mysql](https://github.com/vapor/fluent-mysql) package to your project.
To use MongoDB with Fluent, you just need to make sure to add the [vapor/fluent-mongodb](https://github.com/vapor/fluent-mongodb) package to your project.
To do this, add the Fluent MySQL package to your Package manifest.
@ -47,21 +47,21 @@ let package = Package(
name: "Project",
dependencies: [
...
.package(url: "https://github.com/vapor/fluent-mysql.git", .revision("beta")),
.package(url: "https://github.com/vapor/fluent-mongodb.git", .revision("beta")),
],
targets: [
.target(name: "Project", dependencies: ["FluentMySQL", ... ])
.target(name: "Project", dependencies: ["FluentMongoDB", ... ])
]
)
```
If this is your first time adding a dependency, you should read our introduction to [Package.swift](../../getting-started/spm.md).
Use `import FluentMySQL` to access MySQL's Fluent compatible APIs. [Learn more about the Fluent APIs here](../../fluent/getting-started/provider.md)
Use `import FluentMongoDB` to access MongoDB's Fluent compatible APIs. [Learn more about the Fluent APIs here](../../fluent/getting-started/provider.md)
### Setting up services
In order to set up MySQL with Fluent you first need to register the Fluent provider:
<!-- ### Setting up services -->
<!--
In order to set up MongoDB with Fluent you first need to register the Fluent provider:
```swift
try services.provider(FluentProvider())
@ -104,6 +104,6 @@ Fluent `Model`s that conform to the protocol `Migration` automatically inherit a
var migrationConfig = MigrationConfig()
migrationConfig.add(model: MyModel.self, database: .mysql)
services.instance(migrationConfig)
```
``` -->
More info on working with Fluent can be found [here](../../fluent/getting-started/getting-started.md).

View File

@ -77,12 +77,23 @@ For inserting you can use `insert` which have various parameters which you can u
Most parameters have a default and can thus be left out of not needed.
```swift
myCollection.insert([
"_id": ObjectId(),
"some": "string",
"date": Date()
])
```
## Aggregates
If you followed this article you should know the basics of MongoKitten aggregates. If you, at any point, need a specific stage, you can look for the stage by the MongoDB name in the documentation or make use of autocomplete.
All aggregates can be quickly accessed using by simply typing a dot (.) inside the pipeline array literal like you would for enum cases. This will autocomplete to all available static functions that can help you create a stage. If you miss a stage or want to create a stage manually you can do so by providing a BSONDocument like this:
If you followed this article you should know the basics of MongoKitten aggregates.
If you, at any point, need a specific stage, you can look for the stage by the MongoDB name in the documentation or make use of autocomplete.
All supported aggregates can be quickly accessed using by simply typing a dot (`.`) inside the pipeline array literal like you would for enum cases.
This will autocomplete to all available static functions that can help you create a stage.
If you miss a stage or want to create a stage manually you can do so by providing a BSON `Document` like this:
```swift
let stage = Stage([
"$match": [
"username": [
@ -90,18 +101,21 @@ let stage = Stage([
]
]
])
```
The stage must be equal to the Document representation of the stage operators described here.
You can then add this stage as one of the values in the array.
Queries
MongoDB queries can work if converted to a Document. If you do this, you must either use a literal query like below or write it using the query builder described here.
## Document Queries
MongoDB queries can work if converted to a Document.
If you do this, you must either use a literal query like below or write it using the query builder described here.
```swift
let query = Query(document)
let literalQuery: Query = [
"age": [
"$gte": 15
]
]
Sorting and Projecting
Sorts and projections have quite a bit of sugarcoating around them for readability and MongoDB-newcomers as explained here.
Both objects can be wrapped around a Document like you can with Query :
let sort = Sort(document)
let projection = Projection(document)
```