Basic CURD

Overview

This section describes the relatively complex way to introduce the CURD in the mon package.

Preparing

  1. Complete mongo connection
  2. Basic CURD。

Create New

1 InsertMany

  1. Function signature:
  2. InsertMany func(ctx context.Context, documents []interface{}, opts ...*mopt.InsertManyOptions) (
  3. *mongo.InsertManyResult, error)
  4. description:
  5. 1. Add a single document record.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. type User struct {
  15. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  16. // TODO: Fill your own fields
  17. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  18. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  19. }
  20. func (m *defaultUserModel) InsertMany(ctx context.Context, data []*User) error {
  21. var docs = make([]interface{}, 0, len(data))
  22. for _, d := range data {
  23. if d.ID.IsZero() {
  24. d.ID = primitive.NewObjectID()
  25. d.CreateAt = time.Now()
  26. d.UpdateAt = time.Now()
  27. }
  28. docs = append(docs, d)
  29. }
  30. _, err := m.conn.InsertMany(ctx, docs)
  31. return err
  32. }

2 BulkInserter

  1. Function signature:
  2. NewBulkInserter(coll Collection, interval ...time.Duration) (*BulkInserter, error)
  3. description:
  4. 1. Use if large quantities of new data are present.
  5. Insert the process by bulk (1000) or cycle time.
  6. inputs:
  7. 1. coll: mongo connectin NewObjectID
  8. 2. interval: Batch insertion period, intervals[0] is a valid value
  9. returns:
  10. 1. *BulkInserter: bulk module object
  11. 2. error: Create results
  12. Example:
  13. type User struct {
  14. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  15. // TODO: Fill your own fields
  16. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  17. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  18. }
  19. // NewUserModel returns a model for the mongo.
  20. func NewUserModel(url, db, collection string) UserModel {
  21. conn := mon.MustNewModel(url, db, collection)
  22. blk, err := mon.NewBatchInserter(conn.Collection, time.Second)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. return &customUserModel{
  27. defaultUserModel: newDefaultUserModel(conn),
  28. blk: blk,
  29. }
  30. }
  31. func (m *customUserModel) BatchInsert(ctx context.Context, data []*User) error {
  32. m.blk.SetResultHandler(func(result *mongo.InsertManyResult, err error) {
  33. if err != nil {
  34. log.Println(err)
  35. }
  36. })
  37. for _, d := range data {
  38. if d.ID.IsZero() {
  39. d.ID = primitive.NewObjectID()
  40. d.CreateAt = time.Now()
  41. d.UpdateAt = time.Now()
  42. }
  43. m.blk.Insert(d)
  44. }
  45. m.blk.Flush()
  46. return nil
  47. }

Update

  1. UpdateOne
  1. Function signature:
  2. UpdateOne (ctx context.Context, filter, update interface{},
  3. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  4. description:
  5. 1. Update a single document record.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. type User struct {
  15. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  16. // TODO: Fill your own fields
  17. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  18. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  19. }
  20. func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
  21. data.UpdateAt = time.Now()
  22. _, err := m.conn.UpdateOne(ctx, bson.M{"_id": data.ID}, data)
  23. return err
  24. }

2 UpdateByID

  1. Function signature:
  2. UpdateByID (ctx context.Context, id, update interface{},
  3. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  4. description:
  5. 1. Update individual document records by _id.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. type User struct {
  15. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  16. // TODO: Fill your own fields
  17. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  18. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  19. }
  20. func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
  21. data.UpdateAt = time.Now()
  22. _, err := m.conn.UpdateByID(ctx, data.ID, data)
  23. return err
  24. }

3 UpdateMany

  1. Function signature:
  2. UpdateMany (ctx context.Context, filter, update interface{},
  3. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  4. description:
  5. 1. More document records.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. type User struct {
  15. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  16. Name string `bson:"name,omitempty" json:"name,omitempty"`
  17. Age int `bson:"age,omitempty" json:"age,omitempty"`
  18. // TODO: Fill your own fields
  19. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  20. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  21. }
  22. func (m *customUserModel) UpdateAge(ctx context.Context, name string, age int) error {
  23. _, err := m.conn.UpdateMany(ctx, bson.M{"name": name}, bson.M{"$set": bson.M{"age": age}})
  24. return err
  25. }

Query

1 FindOne

  1. Function signature:
  2. FindOne func(ctx context.Context, v, filter interface{}, opts ...*mopt.FindOneOptions)
  3. note:
  4. . Query individual document records.
  5. Input:
  6. 1. ctx: context
  7. 2. v: record result
  8. 2. filter: filter condition
  9. 3. opts: operating options
  10. return value:
  11. 1. error: Results of the
  12. Example:
  13. type User struct {
  14. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  15. // TODO: Fill your own fields
  16. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  17. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  18. }
  19. func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error) {
  20. oid, err := primitive.ObjectIDFromHex(id)
  21. if err != nil {
  22. return nil, ErrInvalidObjectId
  23. }
  24. var data User
  25. err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid})
  26. switch err {
  27. case nil:
  28. return &data, nil
  29. case mon.ErrNotFound:
  30. return nil, ErrNotFound
  31. default:
  32. return nil, err
  33. }
  34. }

2 Find

  1. Function signature:
  2. Find func(ctx context.Context, v, filter interface{}, opts ...*mopt.FindOptions) error
  3. description:
  4. 1. Query individual document records.
  5. Input:
  6. 1. ctx: context
  7. 2. v: record result
  8. 2. filter: filter condition
  9. 3. opts: operating options
  10. return value:
  11. 1. error: Results of the
  12. Example:
  13. type User struct {
  14. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  15. // TODO: Fill your own fields
  16. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  17. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  18. }
  19. func (m *defaultUserModel) Find(ctx context.Context, id string) ([]*User, error) {
  20. oid, err := primitive.ObjectIDFromHex(id)
  21. if err != nil {
  22. return nil, ErrInvalidObjectId
  23. }
  24. var data []*User
  25. err = m.conn.Find(ctx, &data, bson.M{"_id": oid})
  26. return data, nil
  27. }

Delete

1 DeleteOne

  1. Function signature:
  2. DeleteOne func(ctx context.Context, filter interface{}, opts ...*mopt.DeleteOptions) (int64, error)
  3. note:
  4. . Add a single document record.
  5. Input:
  6. 1. ctx: context
  7. 2. v: record result
  8. 2. filter: filter condition
  9. 3. opts: operating options
  10. return value:
  11. 1. error: Results of the
  12. Example:
  13. ype User struct {
  14. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  15. // TODO: Fill your own fields
  16. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  17. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  18. }
  19. func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
  20. oid, err := primitive.ObjectIDFromHex(id)
  21. if err != nil {
  22. return ErrInvalidObjectId
  23. }
  24. _, err = m.conn.DeleteOne(ctx, bson.M{"_id": oid})
  25. return err
  26. }

2 DeleteMany

  1. Function signature:
  2. DeleteMany func(ctx context.Context, filter interface{}, opts ...*mopt.DeleteOptions) (
  3. *mongo.DeleteResult, error)
  4. description:
  5. 1. Delete a single document record.
  6. Input:
  7. 1. ctx: context
  8. 2. v: record result
  9. 2. filter: filter condition
  10. 3. opts: operating options
  11. return value:
  12. 1. error: Results of the
  13. Example:
  14. type User struct {
  15. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  16. // TODO: Fill your own fields
  17. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  18. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  19. }
  20. func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
  21. oid, err := primitive.ObjectIDFromHex(id)
  22. if err != nil {
  23. return ErrInvalidObjectId
  24. }
  25. _, err = m.conn.DeleteMany(ctx, bson.M{"_id": oid})
  26. return err
  27. }