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

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

$push string as an array in mongo document

问题

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

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

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"

models.go:

type Time struct {
	Login []string
}

type User struct {
	ID       graphql.ID
	Time     Time
}

schema.graphql:

type Time {
  login: [String!]!
}

type User {
  id:      ID!
  time:    Time!
}

database.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)

但总是遇到相同的错误:

英文:

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:

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"

models.go:

type Time struct {
	Login []string
}

type User struct {
	ID       graphql.ID
	Time     Time
}

schema.graphql:

type Time {
  login: [String!]!
}

type User {
  id:      ID!
  time:    Time!
}

database.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)

I've also tried:

update := bson.D{
  {Key: "$push", Value: bson.D{
    {Key: "time.login", Value: time.Now().Format(time.RFC3339)},
  }},
}
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需要一个值,而不是一个切片:

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

[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:

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

答案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:

确定