mongodb 有官方支持的 go 语言驱动了

beary · · 5245 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

**链接** [https://github.com/mongodb/mongo-go-driver](https://github.com/mongodb/mongo-go-driver) 目前版本号0.0.4,应该还有不少开发工作,不过最近commit挺频繁的。 刚刚试了一下,常用接口都有了 ```go /* db.go */ package db import ( "context" "github.com/mongodb/mongo-go-driver/mongo" "log" ) type collectionMap struct { Account *mongo.Collection } var DB *mongo.Database var C collectionMap func Connect() { conn, e := mongo.Connect(context.Background(), "mongodb://localhost:27017", nil) if e != nil { log.Fatal(e) } DB = conn.Database("go-test") C.Account = DB.Collection("Account") } ``` ```go /* account.go */ package db import ( "context" "github.com/mongodb/mongo-go-driver/bson" "github.com/mongodb/mongo-go-driver/bson/objectid" "time" ) type Account struct { ID objectid.ObjectID `bson:"_id"` Phone string `bson:"phone"` CountryCode string `bson:"countryCode"` CreateAt time.Time `bson:"createAt"` } func FindByPhone(countryCode string, phone string) (account Account) { old := C.Account.FindOne(context.Background(), bson.NewDocument( bson.EC.String("phone", phone), bson.EC.String("countryCode", countryCode), )) old.Decode(&account) return } ```

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

5245 次点击  ∙  1 赞  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传