dousongqiang2585 2018-12-28 16:44
浏览 163

从一个结构很复杂的MongoDB中获取数据

For a project I have to query a Mongo database with a RESTful API in Go.

There is my problem, I get this structure from Mongo.

{
    "_id" : "ObjectID",
    "id" : 1234,
    "project" : {
        "id" : 1,
        "name" : "A project"
    },
    "tracker" : {
        "id" : 2,
        "name" : "A tracker"
    },
    "status" : {
        "id" : 3,
        "name" : "A status"
    },
    "priority" : {
        "id" : 4,
        "name" : "A priority"
    },
    "author" : {
        "id" : 5,
        "name" : "An author"
    },
    "assigned_to" : {
        "id" : 6,
        "name" : "Assigned to"
    },
    "category" : {
        "id" : 7,
        "name" : "A category"
    },
    "subject" : "A subject",
    "description" : "A description",
    "done_ratio" : 0,
    "spent_hours" : 0.0,
    "total_spent_hours" : 0.0,
    "custom_fields" : [ 
        {
            "id" : 1,
            "name" : "",
            "value" : ""
        }, 
        {
            "id" : 2,
            "name" : "",
            "multiple" : true,
            "value" : [ 
                ""
            ]
        }

    ],
    "created_on" : "2018-12-21T15:22:57.000Z",
    "updated_on" : "2018-12-26T10:58:35.000Z",
    "journals" : [ 
        {
            "id" : 1,
            "user" : {
                "id" : 1,
                "name" : ""
            },
            "notes" : "",
            "created_on" : "2018-12-21T15:32:51Z",
            "details" : [ 
                {
                    "property" : "",
                    "name" : "",
                    "new_value" : ""
                }
            ]
        }, 
        {
            "id" : 2,
            "user" : {
                "id" : 1,
                "name" : ""
            },
            "notes" : "",
            "created_on" : "2018-12-21T15:33:54Z",
            "details" : []
        }
    ]
}

As you can see, there is two elements called custom_fields and journals which are list of object. But sometimes it can be only a unique object not a list.

In my Go API, I created the following structure:

type Custom struct {
    Id int      `json:"id" bson:"id"`
    Name string `json:"name" bson:"name"`
    Multiple bool `json:"multiple" bson:"multiple"`
    Value []string `json:"value" bson:"value"`
}

type Journal struct {
    ID   int `json:"id" bson:"id"`
    User struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"user" bson:"user"`
    Note       string `json:"notes" bson:"notes"`
    Created_on string `json:"created_on" bson:"created_on"`
    Details    struct {
        Property  string `json:"property" bson:"property"`
        Name      string `json:"name" bson:"name"`
        Old_Value string `json:"old_value" bson:"old_value"`
        New_Value string `json:"new_value" bson:"new_value"`
    } `json:"details" bson:"details"`
}

type MainStruct struct {
    ID      int `json:"id" bson:"id"`
    Project struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"project" bson:"project"`
    Tracker struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"tracker" bson:"tracker"`
    Status struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"status" bson:"status"`
    Priority struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"priority" bson:"priority"`
    Author struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"author" bson:"author"`
    Assigned_to struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"assigned_to" bson:"assigned_to"`
    Subject      string    `json:"subject" bson:"subject"`
    Description  string    `json:"description" bson:"description"`
    Due_date     time.Time `json:"due_date" bson:"due_date"`
    Done_ratio   int       `json:"done_ratio" bson:"done_ratio"`
    CustomFields []Custom  `json:"custom_fields" bson:"custom_fields"`
    Created_on   time.Time `json:"created_on" bson:"created_on"`
    Updated_on   time.Time `json:"updated_on" bson:"updated_on"`
    Journals     []Journal `json:"journals" bson:"journals"`
}

But when I query MongoDb and get a document which as one custom_field or one journal, I get the following errors:

cannot decode string into a slice

I don't know how to do to make the one element into a slice of 1.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?