草庐IT

gorm-mongodb

全部标签

go - 如何将gorm查询结果转换为dict?

查询err:=app.DB.First(&live,1).Error我想把live转换成dict,怎么做? 最佳答案 你可以用反射来做这个。检查这个post。或者,您可以使用structs包轻松转换。看看structmethods部分。 关于go-如何将gorm查询结果转换为dict?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/47070237/

postgresql - Gorm 和关系

我是Golang的新手,对Gorm更是如此。我正在尝试建立一对多的关系。基本上有一个股票代码和他的许多股票数据。一切都很好,除了我无法在模型上建立关系。我只需要一点关于如何做到这一点的提示(欢迎最佳实践)。主.gopackagemainimport("github.com/jinzhu/gorm""github.com/khwerhahn/quoteworker/common""github.com/khwerhahn/quoteworker/tickerdata""github.com/khwerhahn/quoteworker/ticker")//Migratedatabasefu

mongodb - Golang mgo 获取空对象

我正在尝试学习GoAPI开发。我有一个在Docker容器中运行的MongoDB实例。我正在尝试遵循一些指南,但在简单查询时失败了。我不完全理解这里的BSON和JSON标签的使用。我知道这些术语的意思。所以这是我的代码。import("fmt""time""gopkg.in/mgo.v2/bson")const(hosts="localhost:27017"database="my_database"username="dev1"password="password123"collection="users")typeusersstruct{userstring`bson:"user"j

go - 如何在 Gorm 中正确执行部分更新?

我有某种虚拟文件系统。文件夹列表,每个文件夹包含文件和ACL。所以结构看起来像这样:typeModelstruct{IDuint`gorm:"primary_key"json:"id"`}typeUserstruct{ModelNamestring}typeFolderstruct{ModelNamestring}typeFilestruct{ModelNamestringFolderIDuintFolderFolderAcl[]User`json:"acl"gorm:"many2many:file_acl"`}它作为完整的JSON来自前端。它被解码了。当我调用Save(&file)时

Gorm 不创建条目

我是新来的,我有点困惑,为什么会这样:acccountTypeModelWallet:=accounttypes.AccountType{Type:"wallet",Name:"Wallet"}ifdb.First(&acccountTypeModelWallet).RecordNotFound(){fmt.Println("AccountType`Wallet`notfound.Creating..")err:=db.Create(&acccountTypeModelWallet).Erroriferr!=nil{returnfalse,err}}acccountTypeModelB

mongodb - 使用 Gin 和 JobRunner

我正在使用JobRunner用于我的Golang项目的调度程序和Gin-gonic框架。我已经尝试过这个包并且它有效。问题是,当我想连接到我的数据库时,我无法访问调度函数中的gin上下文funcScheduleUrl(){jobrunner.Start()//optional:jobrunner.Start(poolint,concurrentint)(10,1)jobrunner.Schedule("@every20s",CheckExpiredUrl{})}//JobSpecificFunctionstypeCheckExpiredUrlstruct{//filtered}//Re

mongodb 聚合在 golang 中给出错误

我想要分组方法类型并根据它们的类型对它们进行计数。这些方法是字符串。我写了下面的代码。但是它给出了一个错误。pipeline:=[]bson.D{bson.D{{"$unwind","$method"},},bson.D{{"$group",bson.M{"_id":"$method","count":bson.M{"$sum":1}}},},query:=bson.D{{"aggregate","API_ACCESS_LOGS"},//useragentsisacollectionname{"pipeline",pipeline},}err=session.DB("vamps-log

mongodb - 使用 gin-golang 将数据从 mongodb 显示到 html 页面?

我正在尝试检查MongoDB是否包含具有特定用户名和密码的数据。但是现在我得到的是所有数据而不是指定的数据。下面是我的代码:r.POST("/login",func(c*gin.Context){logedUser:=[]RegisterdUser{}name:=c.PostForm("userName")Password:=c.PostForm("userPassword")fmt.Println("insidecheckingsectionnnn",name,Password)session,err:=mgo.Dial("localhost:27017")iferr!=nil{pa

postgresql - 使用 gorm 从两个表中选择

我在一个副项目中使用gorm和postgresql,但我不知道如何在1个请求中解决这个问题:我的用户类型如下:typeUserstruct{UserIdstringEmailstringFirstNamestringLastNamestringPhonestringAddress_1stringAddress_2stringCitystringStatestringZipstringGenderstringImageUristringRoles[]Role`gorm:"foreignkey:UserId"`}我的角色类型如下:typeRolestruct{UserIdstringRol

mongodb - REST API + MongoDb ISODate

我的GoLang结构:typemyPojostruct{IDbson.ObjectId`json:"id"bson:"_id,omitempty"`Starttime.Time`json:"start"`}POSTAPIJSON输入请求:{"Start":ISODate("2013-10-01T00:00:00.000Z")}我将输入JSON请求转换为Golang结构的代码:funcmyPostApi(whttp.ResponseWriter,r*http.Request,dbmongoDB){w.Header().Set("Content-Type","application/jso