Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Added Patch method for the database. #270

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from

Conversation

hsiehby
Copy link
Member

@hsiehby hsiehby commented Nov 20, 2019

I added Patch method in mongo_database.go to enable updates of only the selected fields in an entry.
This method can be used by the patch methods in endpoints such as registration, blobstore, etc.
I also modified the Database interface in database.go to reflect the change.

Copy link
Contributor

@benpankow benpankow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, a few notes inline.

/*
Finds an item based on the given selector and patches it with the data in update
*/
func (db *MongoDatabase) Patch(collection_name string, selector interface{}, update *map[string]interface{}) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not a lot of extra effort, it would be nice to have update be an interface{} to match the signature of the other database functions. Not a big deal if not.

collection := current_session.DB(db.name).C(collection_name)

// Use mongodb set operator to update only the provided fields.
err := collection.Update(selector, bson.M{"$set": update})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's my understanding that this will only merge top-level changes, not nested fields. If so, it might be worth noting this in the comment above.

ie

{
  "foo": {
    "bar1": 1,
  }
}

merged with

{
  "foo": {
    "bar2": 2,
  }
}

results in

{
  "foo": {
    "bar2": 2,
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants