Kamva / mgm

Mongo Go Models (mgm) is a fast and simple MongoDB ODM for Go (based on official Mongo Go Driver)
Apache License 2.0
754 stars 63 forks source link

Mgm.Update is not working properly #68

Closed vigneshEunimart closed 2 years ago

vigneshEunimart commented 2 years ago

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):

mehran-prs commented 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?

vigneshEunimart commented 2 years ago

// 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 ?

mehran-prs commented 2 years ago
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)
    }
}
vigneshEunimart commented 2 years ago

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 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) } }

— 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