Closed vigneshEunimart closed 2 years ago
Hi @vigneshEunimart Could you provide an example code that reset the date fields and another example of the feature that you want to have?
// My model structure
type User struct {
mgm.DefaultModel bson:",inline"
Name string json:"name"
Age string json:"age"
Gender string json:"gender"
Address json:"address"
Created_at time.Time json:"-" bson:",omitempty"
Updated_at time.Time json:"-" bson:",omitempty"
}
type Address struct {
State string json:"state"
Country string json:"country"
}
app.Post("/update", auth.Verify, func(c *fiber.Ctx) error {
var u models.User
c.BodyParser(&u)
filter := fiber.Map{ "name": c.Query("name"), }
// here I am manually updating the updated time. u.Updated_at = time.Now()
res, err := mgm.CollectionByName("users_info").UpdateOne(context.Background(), filter, bson.M{"$set": u}) if err != nil { log.Fatal(err) }
return c.Status(fiber.StatusOK).JSON(fiber.Map{ "status": true, "message": "updated successfully", "data": res, }) })
// How to write this update api using mgm.update function ?
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)
}
}
Before I directly sent the parsed data to the mgm.Update function. so the data won't get updated. Now it's working fine. Thank you
On Tue, Mar 29, 2022 at 7:45 PM Mehran Poursadeghi @.***> wrote:
type User struct { mgm.DefaultModel
bson:",inline"
Name stringjson:"name"
Age stringjson:"age"
Gender stringjson:"gender"
Addressjson:"address"
// Remove data fields, Don't need to them when using the DefaultModel } type Address struct { State stringjson:"state"
Country stringjson:"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) } }
— Reply to this email directly, view it on GitHub https://github.com/Kamva/mgm/issues/68#issuecomment-1081927014, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVEN3AKLHE3UNRCF6UTNZ6LVCMF75ANCNFSM5R2IYOMQ . You are receiving this because you were mentioned.Message ID: @.***>
-- https://eunimart.com/ VIGNESH S
Software Developer - Intern M - +91 6369192234 E - @. @.> W - www.eunimart.com https://eunimart.com/ https://www.linkedin.com/company/eunimartltd/mycompany/ https://www.instagram.com/eunimartltd/ https://www.facebook.com/eunimartltd https://www.youtube.com/channel/UCmGtodAnLo8OeKHjgq6dBGQ Sign up for Vdezi http://eunimart.com/emailredirect
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:
Environment (please complete the following information):