简体   繁体   English

Go-MongoDriver 奇怪地解码 JSON 数组

[英]Go-MongoDriver decoding JSON array weirdly

Heyho party people,嘿嘿党的人,

I've recently took up learning Go and started working on a small side project which includes a API written using the Go Fiber library.我最近开始学习 Go 并开始从事一个小型项目,其中包括使用 Go Fiber 库编写的 API。 All the necessery data is stored in MongoDB with the following schema所有必需的数据都存储在 MongoDB 中,其架构如下

{
    "ObjectId": {
        "name": "name",
        "url": "url",
        "dateAdded": "date",
        "data": [{
                "timestamp 1": "price 1"
            },
            {
                "timestamp 2": "price 2"
            }
        ]
    }
}

The item type looks like this:项目类型如下所示:

type Item struct {
    ID        primitive.ObjectID `json:"_id" bson:"_id"`
    Name      string             `json:"name" bson:"name"`
    URL       string             `json:"url" bson:"url"`
    DateAdded string             `json:"dateAdded" bson:"dateAdded"`
    Data      []interface{}      `json:"data" bson:"data"`
}

Whenever I query a stored item with每当我查询存储的项目时

err = collection.FindOne(context.TODO(), filter).Decode(&item)

each map inside of the data-array is wrapped in another array =>数据阵列内部的每个 map 都包装在另一个阵列中 =>

{ test url 2021-04-16 [[{2021-04-16 99.99}] [{2021-04-17 109.99}]] }

instead of

{ test url 2021-04-16 [{2021-04-16 99.99}, {2021-04-17 109.99}] }

Does anybody have an idea on how to fix this?有人知道如何解决这个问题吗? Thanks in advance!提前致谢!

OK, I've found a way to fix this behaviour for my use case.好的,我找到了一种方法来解决我的用例的这种行为。

As mentioned above, the MongoDB-driver for Go wraps each entry of an array into another respective array, which leads to a nested array.如上所述,Go 的 MongoDB 驱动程序将数组的每个条目包装到另一个相应的数组中,这导致嵌套数组。

After trying around for some time I found out, that inserting the document into your collection like the following example,在尝试了一段时间后,我发现将文档插入到您的集合中,如下例所示,

db.collection.insertOne({ name: "Name", url: "URL", dateAdded: "2021-04-25", data: { "2021-04-25": 9.99, "2021-04-26": 19.99 } })

then the result of a query performed in your program looks like this:那么在您的程序中执行的查询结果如下所示:

{ ObjectID("60858245f8805dc57a716500") Name URL 2021-04-25 [{ 2021-04-25 9.99 } { 2021-04-26 19.99 }] }

This means, that the JSON-schema should look like this这意味着,JSON 模式应该是这样的

{
    "ObjectId": {
        "name": "name",
        "url": "url",
        "dateAdded": "date",
        "data": {
            "2021-04-25": 9.99,
            "2021-04-26": 19.99
        }
    }
}

Sadly, I was not able to find out what is actually causing this odd behaviour, but I hope this helps anybody encountering this (or a similar) problem.遗憾的是,我无法找出导致这种奇怪行为的真正原因,但我希望这可以帮助遇到这个(或类似)问题的任何人。

EDIT编辑

Changing the type of the Data -field to []bson.M , as mkopriva said in the comments below, fixed it.正如mkopriva在下面的评论中所说,将Data -field 的类型更改为[]bson.M ,修复了它。

type Item struct {
    ID        primitive.ObjectID `json:"_id" bson:"_id"`
    Name      string             `json:"name" bson:"name"`
    URL       string             `json:"url" bson:"url"`
    DateAdded string             `json:"dateAdded" bson:"dateAdded"`
    Data      []bson.M           `json:"data" bson:"data"`
}

This way the original JSON-schema does not have to be adapted to the workaround.这样,原始 JSON 模式就不必适应解决方法。

{
    "ObjectId":{
        "name":"name",
        "url":"url",
        "dateAdded":"date",
        "data": [
            {
                "2021-04-25": 9.99
            },
            {
                "2021-04-26": 19.99
            }
        ]
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM