[GODRIVER-1424] Question - How to perform partial update on a collection? Created: 04/Dec/19  Updated: 17/Dec/19  Resolved: 17/Dec/19

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Gabriele Bassi Assignee: Divjot Arora (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Hi to all

I would like to know how to perform a partial update / new insert on a collection?

I have a update end point in patch that contains only some property of a collection with different kind of properties.

Example i have a collection like this

https://jsoneditoronline.org/?id=5d1cb75ba2de4626ab2a4628e29b955e

Update end-point request body

https://jsoneditoronline.org/?id=81f6bbd30bab4349964d692fc3b374ee

 

Update query in go:

 var docs models.Events var docs models.Events if err = json.Unmarshal(bodyBytes, &docs); err != nil { return nil, err }
 
newObj := bson.M{"$set": docs} newObj := bson.M{"$set": docs}
 result, err := collection.UpdateOne(context.Background(), bson.M{"_id": _id}, newObj) if err != nil { return nil, err } fmt.Print(result.ModifiedCount)

In this case all old content of assets are deleted.

Is there a way to perform a partial update / insert without deleting old records?



 Comments   
Comment by Divjot Arora (Inactive) [ 17/Dec/19 ]

Hi gabriele.bassi@yahoo.it,

From what I understand, you want to use "$set" for non-document fields but some sort of merge operator for document fields. I don't think this can be done as a single update, but you can likely do this using an aggregation pipeline with a $mergeObjects stage (https://docs.mongodb.com/manual/reference/operator/aggregation/mergeObjects/). The last stage in the pipeline can be $out or $merge to write the results to an output collection.

Also, the GODRIVER project is for bugs and feature requests for the MongoDB Go Driver specifically. Given that this is a question about the query language, not the driver, I suggest using the mongodb-user Google group instead (https://groups.google.com/forum/#!forum/mongodb-user).

 

– Divjot

Generated at Thu Feb 08 08:36:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.