Basic CURD

Overview

This section presents a description of the methods associated with the mon package.

Preparing

  1. Complete mon link creation.

Create New

  1. InsertOne
  1. Function signature:
  2. InsertOne function (ctx context.Context, document interface{}, opts ...mmopt.InsertOneOptions) (
  3. *mongo.InsertOneResult, 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) Insert(ctx context.Context, data *User) error {
  21. if data.ID.IsZero() {
  22. data.ID = primitive.NewObjectID()
  23. data.CreateAt = time.Now()
  24. data.UpdateAt = time.Now()
  25. }
  26. _, err := m.conn.InsertOne(ctx, data)
  27. return err
  28. }

Update

  1. ReplaceOne
  1. Function signature:
  2. ReplaceOne function (ctx context.Context, filter, replacement interface{},
  3. opts ...mopt.ReplaceOpts)(*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) Insert(ctx context.Context, data *User) error {
  21. if data.ID.IsZero() {
  22. data.ID = primitive.NewObjectID()
  23. data.CreateAt = time.Now()
  24. data.UpdateAt = time.Now()
  25. }
  26. _, err := m.conn.InsertOne(ctx, data)
  27. return err
  28. }

Query

  1. FindOne
  1. Function signature:
  2. FindOne function (ctx context.Context, v, filter interface{}, opts ...mmopt.FindOneOptions) error
  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. }
  1. 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) 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. }

Delete

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

Full demo instance

  1. package main
  2. import (
  3. "time"
  4. "go.mongodb.org/mongo-driver/bson"
  5. "go.mongodb.org/mongo-driver/bson/primitive"
  6. "github.com/zeromicro/go-zero/core/stores/mon"
  7. )
  8. type User struct {
  9. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  10. Username string `bson:"username,omitempty" json:"username,omitempty"`
  11. Password string `bson:"password,omitempty" json:"password,omitempty"`
  12. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  13. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  14. }
  15. func main() {
  16. conn := mon.MustNewModel("mongodb://<user>:<password>@<host>:<port>", "db", "collection")
  17. ctx := context.Background()
  18. u := &User{
  19. ID: primitive.ObjectID{},
  20. Username: "username",
  21. Password: "password",
  22. UpdateAt: time.Now(),
  23. CreateAt: time.Now(),
  24. }
  25. // insert one
  26. _, err := conn.InsertOne(ctx, u)
  27. if err != nil {
  28. panic(err)
  29. }
  30. var newUser User
  31. err = conn.FindOne(ctx, &newUser, bson.M{"_id": u.ID})
  32. if err != nil {
  33. panic(err)
  34. }
  35. newUser.Username = "newUsername"
  36. _, err = conn.ReplaceOne(ctx, bson.M{"_id": newUser.ID}, newUser)
  37. if err != nil {
  38. panic(err)
  39. }
  40. _, err = conn.DeleteOne(ctx, bson.M{"_id": newUser.ID})
  41. if err != nil {
  42. panic(err)
  43. }
  44. }