广汽安道拓Acura项目MES后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
587 B

  1. package db
  2. import (
  3. "LAPP_ACURA_MOM_BACKEND/conf"
  4. "context"
  5. "fmt"
  6. "go.mongodb.org/mongo-driver/mongo"
  7. "go.mongodb.org/mongo-driver/mongo/options"
  8. "time"
  9. )
  10. var AppMongo *mongo.Client
  11. func InitMongo() error {
  12. //1.建立链接
  13. ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
  14. dsn := fmt.Sprintf("mongodb://%s:%d", conf.DbConfig.Mongdbip, conf.DbConfig.Mongdbport)
  15. client, err := mongo.Connect(ctx, options.Client().ApplyURI(dsn))
  16. if err != nil {
  17. return err
  18. }
  19. if err = client.Ping(ctx, nil); err != nil {
  20. return err
  21. }
  22. AppMongo = client
  23. return err
  24. }