0
点赞
收藏
分享

微信扫一扫

go 链接mongo db


package db

import (
"context"
"github.com/mongodb/mongo-go-driver/mongo"
"time"
)

/* mongodb */
func InitMongoDB() (collection *mongo.Database, err error){
const url = "mongodb://192.168.1.108:27017";
const dbName="mycol"
client,err :=mongo.NewClient(url)
if err != nil{
return nil,err
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
return nil, err
}
collection = client.Database(dbName)
return collection, nil
}

 

举报

相关推荐

0 条评论