在MongoDB文档中将字符串作为数组推送。

huangapple go评论120阅读模式
英文:

$push string as an array in mongo document

问题

我在Go语言中遇到了一些理解数组的问题,特别是在使用GraphQL和MongoDB时。在JavaScript中,这对我来说都很容易,但我想知道你是否可以看看我的代码并指出明显的错误!

我希望对象的结构如下所示:

  1. time
  2. ├── login
  3. ├── 0
  4. └── "2021-08-16T20:11:54-07:00"
  5. ├── 1
  6. └── "2021-08-16T20:11:54-07:00"
  7. └── 2
  8. └── "2021-08-16T20:11:54-07:00"

models.go:

  1. type Time struct {
  2. Login []string
  3. }
  4. type User struct {
  5. ID graphql.ID
  6. Time Time
  7. }

schema.graphql:

  1. type Time {
  2. login: [String!]!
  3. }
  4. type User {
  5. id: ID!
  6. time: Time!
  7. }

database.go:

  1. filter := bson.D{{Key: "email", Value: email}}
  2. arr := [1]string{time.Now().Format(time.RFC3339)}
  3. update := bson.D{
  4. {Key: "$push", Value: bson.D{
  5. {Key: "time.login", Value: arr},
  6. }},
  7. }
  8. result, err := collection.UpdateOne(ctx, filter, update)

我还尝试过:

  1. update := bson.D{
  2. {Key: "$push", Value: bson.D{
  3. {Key: "time.login", Value: time.Now().Format(time.RFC3339)},
  4. }},
  5. }
  6. result, err := collection.UpdateOne(ctx, filter, update)

但总是遇到相同的错误:

英文:

I'm having trouble understanding arrays in Go, especially with both graphql and mongo. With JS this all would've been a breeze for me, but I'm wondering if you can look at what I have and point out the obvious!

I'm looking for an object to be shaped like the following:

  1. time
  2. ├── login
  3. ├── 0
  4. └── "2021-08-16T20:11:54-07:00"
  5. ├── 1
  6. └── "2021-08-16T20:11:54-07:00"
  7. └── 2
  8. └── "2021-08-16T20:11:54-07:00"

models.go:

  1. type Time struct {
  2. Login []string
  3. }
  4. type User struct {
  5. ID graphql.ID
  6. Time Time
  7. }

schema.graphql:

  1. type Time {
  2. login: [String!]!
  3. }
  4. type User {
  5. id: ID!
  6. time: Time!
  7. }

database.go:

  1. filter := bson.D{{Key: "email", Value: email}}
  2. arr := [1]string{time.Now().Format(time.RFC3339)}
  3. update := bson.D{
  4. {Key: "$push", Value: bson.D{
  5. {Key: "time.login", Value: arr},
  6. }},
  7. }
  8. result, err := collection.UpdateOne(ctx, filter, update)

I've also tried:

  1. update := bson.D{
  2. {Key: "$push", Value: bson.D{
  3. {Key: "time.login", Value: time.Now().Format(time.RFC3339)},
  4. }},
  5. }
  6. result, err := collection.UpdateOne(ctx, filter, update)

But always end up with the same error:

error="write exception: write errors: [The field 'time.login' must be an array but is of type null in document {_id: ObjectId('611b28fabffe7f3694bc86dc')}]"

答案1

得分: 0

[1]string{} 是一个数组。[]string{} 是一个切片。这两者是不同的。数组是一个固定大小的对象。切片可以动态地增长/缩小。

在这种情况下,你不应该使用任何一个,因为$push需要一个值,而不是一个切片:

  1. update := bson.D{
  2. {Key: "$push", Value: bson.D{
  3. {Key: "time.login", Value: time.Now().Format(time.RFC3339)},
  4. }},
  5. }
英文:

[1]string{} is an array. []string{} is a slice. The two are different. An array is a fixed size object. A slice can grow/shrink dynamically.

In this case, you should not be using either, because $push gets a value, not a slice:

  1. update := bson.D{
  2. {Key: "$push", Value: bson.D{
  3. {Key: "time.login", Value: time.Now().Format(time.RFC3339)},
  4. }},
  5. }

答案2

得分: 0

尝试初始化切片。
并使用append。

英文:

Try initializing the slice.
And use append.

huangapple
  • 本文由 发表于 2021年8月17日 11:54:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/68811494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定