mongo_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package mhayaMongo
  2. //
  3. //import (
  4. // "context"
  5. // "fmt"
  6. // "testing"
  7. //
  8. // clog "github.com/mhaya/logger"
  9. // "go.mongodb.org/mongo-driver/bson"
  10. // "go.mongodb.org/mongo-driver/bson/primitive"
  11. //)
  12. //
  13. //type Student struct {
  14. // Name string
  15. // Age int
  16. //}
  17. //
  18. //func TestConnect(t *testing.T) {
  19. // clog.Info("test connect mongodb")
  20. //
  21. // uri := "mongodb://localhost:27017"
  22. // dbName := "test"
  23. //
  24. // mdb, err := CreateDatabase(uri, dbName)
  25. // if err != nil {
  26. // clog.Warn(err)
  27. // return
  28. // }
  29. //
  30. // collection := mdb.Collection("numbers")
  31. //
  32. // student := &Student{
  33. // Name: "aaa222",
  34. // Age: 111,
  35. // }
  36. //
  37. // res, err := collection.InsertOne(context.Background(), student)
  38. // insertID := res.InsertedID
  39. // clog.Infof("id = %v, err = %v", insertID, err)
  40. //
  41. // //id, _ := primitive.ObjectIDFromHex("649160b6c637f5773cc1e818")
  42. // id, ok := insertID.(primitive.ObjectID)
  43. // if !ok {
  44. // return
  45. // }
  46. //
  47. // findFilter := bson.M{"_id": id}
  48. // findResult := collection.FindOne(context.Background(), findFilter)
  49. //
  50. // findStudent := Student{}
  51. // findResult.Decode(&findStudent)
  52. // fmt.Println(findStudent)
  53. //}