Skip to content

Better ergonomics around "set" functionality in updates #18

@DeanPDX

Description

@DeanPDX

Description

Right now, the go client is using a map:

// Update the document's name
result, err := c.UpdateOne(ctx, filter.F{"_id": insertedID}, map[string]any{
	"$set": map[string]any{"name": "UpdateOneModified"},
})

Wondering if/how we can improve on this.

What do the other clients do?

Here's what the TypeScript equivalent looks like:

const result = await collection.updateOne(
  { _id: "101" },
  { $set: { color: "blue" } },
);

Python:

result = collection.update_one(
    {"_id": "101"}, {"$set": {"color": "blue"}}
)

Java:

// Update a document
Filter filter = Filters.eq("_id", "101");
Update update = Update.create().set("color", "blue");
CollectionUpdateResult result = collection.updateOne(filter, update);

.NET client:

var filter = Builders<SimpleObject>.Filter
    .Eq(so => so.Name, "Cat");
var updater = Builders<SimpleObject>.Update;
var update = updater.Set(so => so.Properties.PropertyTwo, "CatUpdated")
    .Unset("Properties.PropertyOne");

Possible solution

We could provide a wrapper like we do with filters. For example:

updateResult, err := collection.UpdateOne(ctx,
	filter.Eq("title", "The Go Programming Language"),
	filter.Set("year", 2016),
)

This is somewhat similar to how Mongo handles updates:

filter := bson.D{{"species", "Ledebouria socialis"}, {"plant_id", 3}}
update := bson.D{{"$set", bson.D{{"species", "Ledebouria socialis"}, {"plant_id", 3}, {"height", 8.3}}}}
result, err := coll.UpdateOne(context.TODO(), filter, update)

Setter/updater stuff probably doesn't belong in the filter package. But something along these lines just to give helpers and reduce boilerplate on the consumers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions