**链接** [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
}
```
有疑问加站长微信联系(非本文作者)