doudi7782 2019-04-15 20:09
浏览 118
已采纳

为什么我的mongodb查询返回0个结果?

This is my database collection: Documents
With this go code, I try to get all the users who are either involved in the story or created the story with the given id.

func main() {
    for stf.DB == nil {
    }

    collection := stf.DB.Collection("user")
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()

    id, _ := primitive.ObjectIDFromHex("5cb4dd7e29d3dca573a73d4c")
    fter := bson.M{"_id": id}

    involvedFilter := bson.M{"stories_involved": fter}
    createdFilter := bson.M{"stories_created": fter}

    filter := bson.M{"$or": []bson.M{involvedFilter, createdFilter}}

    cur, err := collection.Find(ctx, filter)
    if err != nil {
        log.Fatal(err.Error())
    }

    defer cur.Close(ctx)

    for cur.Next(ctx) {
        var result bson.M
        err := cur.Decode(&result)

        if err != nil {
            log.Fatal(err.Error())
        }
        fmt.Println(result)
    }
    if err := cur.Err(); err != nil {
        log.Fatal(err.Error())
    }
}


The code doesn't output any errors, but it also doesn't output any objects...
Thanks for your help in advance!

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 如何修改为正确的?求解决
      • ¥15 django访问管理员界面问题
      • ¥20 python homework完成
      • ¥20 求解 多变量系统的最小二乘辨识问题的推导以及matlab仿真
      • ¥15 arduino esp8266 Blinker编译报问题
      • ¥15 ubuntu18.04运行模型,直接死机
      • ¥30 (问卷调查)莫名其妙丢了u盘,你们会是什么心理状态
      • ¥100 Spark+android应⽤案例
      • ¥15 yolov8 目标检测 重叠 遮挡
      • ¥20 微信聊天记录如何部署到服务器上