Code Monkey home page Code Monkey logo

mongo-driver's Introduction

Mongo Driver for Fluent

Swift Slack Status

Install the MongoDB server

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

OS X

brew install mongodb

Ubuntu

sudo apt-get update
sudo apt-get install mongodb

Run the MongoDB server

mongod

Connecting to MongoDB with Fluent

You need to edit Config/fluent.json to define mongo as the underlying database technology to use as driver for Fluent:

{ 
    "driver": "mongo" 
}

Creating a driver is done using the MongoDB Connection String URI Format. Initializing a MongoDriver such a URI will attempt a connection to MongoDB.

import MongoDriver
import Fluent

let driver = try MongoDriver("mongodb://localhost")
let db = Fluent.Database(driver)

mongo-driver's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mongo-driver's Issues

Modify is not working properly

I noticed a critical bug in driver behavior. Modify functionality is not working as expected. I think issue is connected to #25 but I want to describe it in more detail. Steps to reproduce (you can run locally example app - https://github.com/voronianski/vapor-server-example/ to repro):

  • save 2 documents in db via endpoint like curl -H "Content-Type: application/json" -X POST -d '{"text":"some"}' POST https://localhost:8080/posts
  • check in database
{ "_id" : ObjectId("5f214f58d66cdc2b53b99b99"), "id" : null, "text" : "some" }
{ "_id" : ObjectId("63214f58d66cdc2b54b99b99"), "id" : null, "text" : "some 2" }
  • send modify request to one of the items curl -H "Content-Type: application/json" -X POST -d '{"text":"some change"}' PATCH https://localhost:8080/posts/63214f58d66cdc2b54b99b99
  • check in database:
{ "_id" : ObjectId("5f214f58d66cdc2b53b99b99"), "id" : "63214f58d66cdc2b54b99b99", "text" : "some change" }
{ "_id" : ObjectId("63214f58d66cdc2b54b99b99"), "id" : null, "text" : "some 2" }

Observe that "text" was updated in a wrong document. It looks like driver makes wrong query to update document and matches first one without id field.

Implementation of Controller and Model:

Issue with vapor relationships and fluent

The issue is that when a relationship is created between two models of Vapor, instead of having fluent define the correct key as modelname_id the database stores as modelname__id. This in turn makes it impossible to POST to that affected models.
screen shot 2017-09-22 at 13 15 17
here is an example of contact model that should have a user_id key but returns an error on POST.

CryptoEssentials Package.swif error

When I add the mongo-driver git to my app's package and try to execute swift build I get the following error:
./Packages/CryptoEssentials.git/Package.swift:5:23: error: argument 'dependencies' must precede argument 'exclude'

NotMasterNoSlaveOk

As described in orlandos-nl/MongoKitten#167

I keep randomly getting this error, not sure what could be the cause:

MongoKitten.MongoError.invalidResponse([{\"ok\":0.0,\"codeName\":\"NotMasterNoSlaveOk\",\"errmsg\":\"not master and slaveOk=false\",\"code\":13435}])

Environment:

  • Mongodb 3.4.14 (Cloud Atlas)
  • MongoKitten 4.0.19

Expected behaviour:

mongo-driver should shutdown MongoKitten's server object and try to reconnect on the next query.

@Joannis

Fluent siblings relationship count always returns zero

Consider two entities A and B, with a many-to-many relationship.

let a = A(id: 9)

let b1 = B(id: 1)
let b2 = B(id: 2)

try a.many_b.add(b1)

try a.many_b.all() -> returns 1 entity
try a.many_b.count() -> returns 0
try a.many_b.makeQuery().count() -> returns 0

From MongoDriver.swift line 241:

...
return try collection.count(filter, limitedTo: limit, skipping: skip).makeNode()
...

I can see that when calling count():

  • collection = MongoKitten.Collection<mongodb://localhost:27017/mydb.b>
  • filter = Query(aqt: MongoKitten.AQT.valEquals(key: "a__id", val: "9"))
  • limit = nil
  • skip = nil

It seems to me that the filter is applied to the B table, which obviously doesn't contain a__id which belongs only to the pivot table Pivot<A, B>.

Not sure this is an issue in fluent or in mongo-driver, and I am also not sure if Fluent siblings are supposed to be used like this.

It could be related to #38

/cc @tanner0101 @loganwright @Joannis

FYI this works:

let aIdKey = Pivot<A, B>.leftIdKey
let total = try Pivot<A, B>.makeQuery().filter(aIdKey, 9).count()
print(total) -> 1

--- Edit:

I think the issue is that the count action implementation in MongoDriver doesn't honour the join that is in the query.

"id" not populated on first save

I have made a model and saved it to a Mongo DB. My model creates "var id: Node?" and init to "self.id = nil". However, when the record is saved I notice Mongo creates a different "_id" with values like this "ObjectId("31943c580047b1f729e088f5")". The "id" field is left null as you can see here:

screen shot 2016-11-28 at 4 16 04 pm

However, if I do the same thing but save the model twice then it seems to populate the "id" field as you can see here:

screen shot 2016-11-28 at 4 47 27 pm

I believe this should have been done on the first save, but if you have multiple documents, it will arbitrarily update the id of the first document it finds (i.e., not necessarily the correct one)

The dependency graph could not be satisfied

I can't build with SPM with version 0.1.0 and version 0.7 of Fluent.
I get the The dependency graph could not be satisfied error from SPM.
There must be some dependency somewhere that requires different versions of the same dependency, but I'm not sure. I also have the same problem using the mysql-driver.

Vapor's TLS as MongoTCP connection

I think that's a good thing to add here. We added it for a reason, and don't use SSL/TLS by default because we try to stay neutral/pure-swift.

Readme not clear

I'm not sure what should I add on my Package.swift file because XCode says no such module MongoDriver. I tried to add mongokitten's and this git repo without luck.

Thanks for the help.

Future improvements

I've compiled a list of suggestions for the mongo-driver that can be added in the future by one or more PRs.

MongoDB version checking for feature levels.

Currently, $lookup is the only method for left-joins and was first introduced in MongoDB 3.2. Although MongoDB versions before 3.2 aren't supportable unless multiple queries are manually sent, MongoKitten exposes version and other build information of the server here and here. This can be used to throw errors without sending information to MongoDB it cannot process and give more accurate errors.

Support for MongoDB operators

MongoDB supports arrays and other recursive (dictionary/array) structures thoroughly. Operators that interact with arrays are almost a necessity and should be added to the query builder.

Support for GeoJSON and Graph queries amongst others

Through aggregates, and commands such as geoNear, many more complicated queries can be supported. It's almost essential for many MongoDB users to be able to rely on tools like this.

Collations and read/write concerns

In MongoDB these aren't part of the schema but part of a query, although read concerns aren't usually important, collations can prove helpful to MongoDB users working with other alphabets and even accents on letters. Write concerns may prove to be important to some specific data sets such as payments in webshops.

Limit not working

When coded it:

guard let search = request.data["search"]?.string else { throw Abort.badRequest }

        let query = try City.query()

        query.limit = Limit(count: 10)

        return try query.filter("name", contains: search).all().sorted(by: { $0.name < $1.name }).makeNode().converted(to: JSON.self)

The query.limit is not working, can help me?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.