vapor-community / mongo-provider

MongoDB Provider for Vapor
40 stars 18 forks source link

Mongodb Fetch Error #1

Open mengxiangyue opened 7 years ago

mengxiangyue commented 7 years ago

I use mongodb code like this:

var user = User(name: "Vapor")
    user.id = Node(42)
    do {
        try user.save()
    } catch let e {
        print(e)
    }
    let u = try User.find(42)
    print("fetch \(u)") // is nil

The u is nil, the fetch query sql is

select("users", [(User) _id equals number(42)], [], [], Optional(Fluent.Limit(count: 1, offset: 0)))

the '_id' is mongodb default key,not my 'id' key of 'User' Model. Is this a Bug?

tanner0101 commented 7 years ago

Could you paste the model code? What you have here should be working.

mengxiangyue commented 7 years ago
final class User: Model, Preparation {
    var name: String
    var age: Int = 2
    var exists: Bool = false
    var id: Node?

    init(name: String) {
        self.name = name
    }

    init(node: Node, in context: Context) throws {
        id = try node.extract("id")
        name = try node.extract("name")
        age = try node.extract("age")
    }

    func makeNode(context: Context) throws -> Node {
        return try Node(node: [
            "id": id,
            "name": name,
            "age": age
            ])
    }

    static func prepare(_ database: Database) throws {
        try database.create("users") { users in
            users.id()
            users.string("name")
            users.int("age")
        }
    }

    static func revert(_ database: Database) throws {
        try database.delete("users")
    }
}

I hope that more detailed documentation

tanner0101 commented 7 years ago

I think the issue is try node.extract("id"). If the id field is _id, you should use _id there.

mengxiangyue commented 7 years ago

In mongodb the data is:

> db.users.find()
{ "_id" : ObjectId("8817eb575f01ea3c4cfc4251"), "id" : NumberLong(42), "name" : "Vapor" }
{ "_id" : ObjectId("7018eb575f01ea3c4dfc4251"), "id" : NumberLong(42), "name" : "Vapor" }
{ "_id" : ObjectId("a818eb5716fdff178abb2792"), "id" : NumberLong(42), "name" : "Vapor" }
{ "_id" : ObjectId("f518eb5759c137b426c63229"), "id" : NumberLong(42), "name" : "Vapor" }
{ "_id" : ObjectId("1419eb5759c137b427c63229"), "id" : NumberLong(42), "name" : "Vapor" }
{ "_id" : ObjectId("281beb57b3a64c37af5b3f3f"), "id" : NumberLong(42), "name" : "Vapor" }
{ "_id" : ObjectId("6a1deb576c9f478495e8e87d"), "id" : NumberLong(42), "name" : "Vapor", "age" : NumberLong(2) }
{ "_id" : ObjectId("5120eb571770378b8f8c5105"), "id" : NumberLong(42), "name" : "Vapor", "age" : NumberLong(2) }
{ "_id" : ObjectId("724deb57d0c4c1bcbd97722e"), "id" : NumberLong(42), "name" : "Vapor", "age" : NumberLong(2) }

I want find with 'id' not '_id', the '_id' is mongodb default cloumn. I'm not good at English.

tanner0101 commented 7 years ago

Oh I see. Then do User.query().filter("id", 42).first()

mengxiangyue commented 7 years ago

It's ok. Where I use find(42) with sqlite return a User model, I want to unify. Thanks!

mengxiangyue commented 7 years ago

I would like to translate the website https://vapor.github.io/documentation/ into Chinese, OK?