douwen1915 2019-04-16 13:43
浏览 131

MongoDB管道(Golang)仅在文档内部返回四个字段中的一个

I was trying to dump data received from a lookup using mgo/pkg package but when I receive data, dump it and return, I get this output:

 [
  {
    "_id": "",
    "timerId": "",
    "content": "task1",
    "creationDate": 0
  },
  {
    "_id": "",
    "timerId": "",
    "content": "task2",
    "creationDate": 0
  },
  {
    "_id": "",
    "timerId": "",
    "content": "task3",
    "creationDate": 0
  }
]

Printing with fmt:

[{ObjectIdHex("") ObjectIdHex("") task1 0} {ObjectIdHex("") ObjectIdHex("") task2 0} {ObjectIdHex("") ObjectIdHex("") task3 0}]  

Code that is performing this action:

func (m *MongoDbRepository) GetTasksOnTheSameDateAsUserTimers(userId string)([]model.Task, error) {

    var tasks []model.Task
    // m.Db.Session.DB("project").C("tasks")
    pipe := m.Db.C("tasks").Pipe([]bson.M{
        // bson.M{
        //  "$lookup": bson.M{
        //      "from":         "times",
        //      "localField":   "timerId",
        //      "foreignField": "_id",
        //      "as":           "timers",
        //  },
        // },
        // bson.M{
        //  "$lookup": bson.M{
        //      "from":         "users",
        //      "localField":   "timers.userId",
        //      "foreignField": "_id",
        //      "as":           "userTasks",
        //  },
        // },
        // bson.M{
        //  "$project": bson.M{
        //      "timers": 0,
        //  },
        // },
        // bson.M{
        //  "$match": bson.M{ 
        //      "_id":bson.ObjectIdHex("5cb58c5c4d11610c20b52f1a"),
        //  },
        // },
    })

    err := pipe.All(&tasks)
    fmt.Println(tasks)
    return tasks, err
}

I have commented all inside pipe because I can't perform the lookup because of I can't get the result of the piping correctly. I know that is a dumping data error because if I change ending lines like this:

var tasksDump []interface{}
err := pipe.All(&tasksDump)
fmt.Println(tasksDump)

Then this is the output:

[map[_id:ObjectIdHex("5cb58c5c4d11610c20b52f1a") content:task1 creationDate:1555400713 timerId:ObjectIdHex("5cb5880891b3fb1e2830b743")] map[_id:ObjectIdHex("5cb58ccd4d11610c20b52f1b") content:task2 creationDate:1555400714 timerId:ObjectIdHex("5cb5880891b3fb1e2830b743")] map[_id:ObjectIdHex("5cb58d074d11610c20b52f1c") content:task3 creationDate:1555400700 timerId:ObjectIdHex("5cb5880891b3fb1e2830b743")]]

So data is arriving but when parse I have the issue. Why only content field is being parsed correctly?

Code of the struct where I am dumping data from lookup:

type Task struct {
    Id          bson.ObjectId       `json:"_id"             bson:"_id"`
    TimerId         bson.ObjectId       `json:"timerId"         bson:"timerId"`
    Content         string          `json:"content"         bson:"content"`
    CreationDate            int64           `json:"creationDate"     bson:"creationDate"`
}

MongoDB collection from where I'm receiving data:

enter image description here

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ogg dd trandata 报错
    • ¥15 高缺失率数据如何选择填充方式
    • ¥50 potsgresql15备份问题
    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错