gitee.com/woood2/luca@v1.0.4/internal/repository/student.go (about)

     1  package repository
     2  
     3  import (
     4  	"context"
     5  	"go.mongodb.org/mongo-driver/mongo"
     6  	"time"
     7  )
     8  
     9  type Student struct {
    10  	Name string
    11  	Age  int
    12  }
    13  
    14  func CreateStudent(ctx context.Context, mongoDB *mongo.Database, s *Student) (id interface{}, err error) {
    15  	c := mongoDB.Collection("student")
    16  	ctX, cancel := context.WithTimeout(ctx, 1*time.Second)
    17  	defer cancel()
    18  	r, e := c.InsertOne(ctX, s)
    19  	if e != nil {
    20  		return nil, e
    21  	}
    22  	return r.InsertedID, nil
    23  }