Open mengxiangyue opened 7 years ago
Could you paste the model code? What you have here should be working.
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
I think the issue is try node.extract("id")
. If the id field is _id
, you should use _id
there.
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.
Oh I see. Then do User.query().filter("id", 42).first()
It's ok. Where I use find(42) with sqlite return a User model, I want to unify. Thanks!
I would like to translate the website https://vapor.github.io/documentation/ into Chinese, OK?
I use mongodb code like this:
The u is nil, the fetch query sql is
the '_id' is mongodb default key,not my 'id' key of 'User' Model. Is this a Bug?