MongoDB GO driver overwrites existing data
php Editor Banana brings an introduction to the MongoDB GO driver’s new coverage of existing data. As a popular NoSQL database, MongoDB is gaining popularity among developers. This new driver provides a flexible and efficient way to manipulate data in MongoDB. It supports a variety of query and update operations while also providing advanced features such as transaction processing and data aggregation. By using this driver, developers can easily interact with MongoDB in GO projects, enabling rapid development and high performance. Whether it's a new or existing project, this driver provides developers with a better data manipulation experience. Whether you are a beginner or an experienced developer, this driver will be your best choice.
Question content
I am using go-fiber
and using the mongodb
mongodb go driver.
I only want to update the fields given by the body. But it is overwriting the data.
func UpdateOneUser(c *fiber.Ctx) error { params := c.Params("id") body := new(models.User) id, err := primitive.ObjectIDFromHex(params) if err != nil { return c.Status(500).SendString("invalid onjectid") } if err := c.BodyParser(&body); err != nil { return c.Status(400).SendString("invalid body") } filter := bson.M{"_id": id} update := bson.M{"$set": bson.M{ "name": body.Name, "username": body.Username, "first_name": body.FirstName, "last_name": body.LastName, "email": body.Email, "phone_number": body.PhoneNumber, "contry": body.Contry, "age": body.Age, "child_accounts": body.ChildAccounts, "groups": body.Groups, }} result, err := db.User.UpdateOne(context.Background(), filter, update) if err != nil { return c.Status(500).SendString("user not found") } fmt.Println(result) return c.JSON(body) }
If this is how the driver works, please tell me a better way to update the documentation.
Workaround
$set
operator will cover all fields you specify, so you must selectively construct the update statement:
fields:=bson.m{} if body.name!="" { fields["name"]=body.name } ... update:=bson.m{"$set":fields}
There are some shortcuts you can use:
fields:=bson.M{} add:=func(key,value string) { if value!="" { fields[key]=value } } add("name",body.Name) add("userName",body.UserName)
The above is the detailed content of MongoDB GO driver overwrites existing data. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

The article explains how to use the pprof tool for analyzing Go performance, including enabling profiling, collecting data, and identifying common bottlenecks like CPU and memory issues.Character count: 159

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

The article discusses managing Go module dependencies via go.mod, covering specification, updates, and conflict resolution. It emphasizes best practices like semantic versioning and regular updates.
