Kamva/mgm

Mgm.Update is not working properly

vigneshEunimart opened this issue · 4 comments

While creating a new data using mgm.create method, its successfully created the data with created at and updated at details in defaultmodel field. But While updating the data using updateOne or FindOneandUpdate, its successfully updated but the created at and Updated at field details became 0001-01-01T00:00:00.000+00:00 in the database. I know, here i am using mongo drive function so that defaultmodel fields are not accessed so that the created at and updated at becames 0001-01-01T00:00:00.000+00:00.

why I am using mongo drive function because while update using mgm.update, there is only one parameter as model. so I
find the data which i need to update and manually map the details and send the parameter to that function. Even though, it's not updating properly in database.

Expected behavior:

  1. please provide an extra filter parameter to update function in mgm. so that it will be easy to use mgm.update function
    Or tell us how to use the mgm.update function, Because the Previous information provided by you is not clear.

Environment (please complete the following information):

  • OS: Ubuntu

Hi @vigneshEunimart
Could you provide an example code that reset the date fields and another example of the feature that you want to have?

type User struct {
	mgm.DefaultModel `bson:",inline"`
	Name             string `json:"name"`
	Age              string `json:"age"`
	Gender           string `json:"gender"`
	Address          `json:"address"`
	// Remove data fields, Don't need to them when using the DefaultModel
}

type Address struct {
	State   string `json:"state"`
	Country string `json:"country"`
}

func updateIt(c *fiber.Ctx) {
	var u User
	filter := bson.M{"name": c.Query("name")}
	err := mgm.CollectionByName("users_info").First(filter, &u)
	if err != nil {
		log.Fatal(err)
	}

	id := u.ID
	c.BodyParser(&u) // Update data
	u.ID = id        // Keep the original ID after getting new data from request

	err = mgm.CollectionByName("users_info").Update(&u)
	if err != nil {
		log.Fatal(err)
	}
}