我无法理解 Go 中的数组,尤其是 graphql 和 mongo。有了 JS,这一切对我来说都是轻而易举的事,但我想知道你是否可以看看我所拥有的并指出显而易见的!
我正在寻找一个形状如下的对象:
time
├── login
│ ├── 0
│ │ └── "2021-08-16T20:11:54-07:00"
│ ├── 1
│ │ └── "2021-08-16T20:11:54-07:00"
│ └── 2
└── "2021-08-16T20:11:54-07:00"
模型.go:
type Time struct {
Login []string
}
type User struct {
ID graphql.ID
Time Time
}
架构.graphql:
type Time {
login: [String!]!
}
type User {
id: ID!
time: Time!
}
数据库.go:
filter := bson.D{{Key: "email", Value: email}}
arr := [1]string{time.Now().Format(time.RFC3339)}
update := bson.D{
{Key: "$push", Value: bson.D{
{Key: "time.login", Value: arr},
}},
}
result, err := collection.UpdateOne(ctx, filter, update)
我也试过:
update := bson.D{
{Key: "$push", Value: bson.D{
{Key: "time.login", Value: time.Now().Format(time.RFC3339)},
}},
}
result, err := collection.UpdateOne(ctx, filter, update)
但总是以同样的错误告终:
error="write exception: write errors: [The field 'time.login' must be an array but is of type null in document {_id: ObjectId('611b28fabffe7f3694bc86dc')}]"