简体   繁体   中英

How to fetchall records in mongodb mgo.v2?

I Want to fetch all records in database By using NAME but, if I am using ALL it is showing 500 internal error but, if I Kept One(JSON) I am getting only one record. What is the solution to fetch all records by Name?

func (uc UserController) Filter(c *gin.Context) {
    var name = c.Params.ByName("Name")
    var json models.User
    err := c.Bind(&json)
    if err != nil {
        log.Fatal("error")
        return
    }
    json.Name = name
    fi := bson.D{{"Name", name}}
    err = uc.session.DB(DB_NAME).C(DB_COLLECTION).Find(fi).All(json)

    if err == nil {
        c.Writer.Header().Set("Content-Type", "application/json")
        c.JSON(201, &json)
    } else {
        c.JSON(500, gin.H{"result": "An error occured"})
    }

}

您应该将一个数组( var json []models.User )传递给All(&json)函数,但您传递的是一项( var json models.User )。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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