This chapter introduces how to update document data in Golang MongoDB.

Update Functions Supported by MongoDB

  • Collection.UpdateOne - Update a single piece of data
  • Collection.UpdateMany - Update data in batches
  • Collection.ReplaceOne - Replace data

Prepare Test Data

Insert a batch of data into the coll collection

docs := []interface{}{
	bson.D{
		{"item", "canvas"},
		{"qty", 100},
		{"size", bson.D{
			{"h", 28},
			{"w", 35.5},
			{"uom", "cm"},
		}},
		{"status", "A"},
	},
	bson.D{
		{"item", "journal"},
		{"qty", 25},
		{"size", bson.D{
			{"h", 14},
			{"w", 21},
			{"uom", "cm"},
		}},
		{"status", "A"},
	},
	bson.D{
		{"item", "mat"},
		{"qty", 85},
		{"size", bson.D{
			{"h", 27.9},
			{"w", 35.5},
			{"uom", "cm"},
		}},
		{"status", "A"},
	},
	bson.D{
		{"item", "mousepad"},
		{"qty", 25},
		{"size", bson.D{
			{"h", 19},
			{"w", 22.85},
			{"uom", "in"},
		}},
		{"status", "P"},
	},
	bson.D{
		{"item", "notebook"},
		{"qty", 50},
		{"size", bson.D{
			{"h", 8.5},
			{"w", 11},
			{"uom", "in"},
		}},
		{"status", "P"},
	},
	bson.D{
		{"item", "paper"},
		{"qty", 100},
		{"size", bson.D{
			{"h", 8.5},
			{"w", 11},
			{"uom", "in"},
		}},
		{"status", "D"},
	},
	bson.D{
		{"item", "planner"},
		{"qty", 75},
		{"size", bson.D{
			{"h", 22.85},
			{"w", 30},
			{"uom", "cm"},
		}},
		{"status", "D"},
	},
	bson.D{
		{"item", "postcard"},
		{"qty", 45},
		{"size", bson.D{
			{"h", 10},
			{"w", 15.25},
			{"uom", "cm"},
		}},
		{"status", "A"},
	},
	bson.D{
		{"item", "sketchbook"},
		{"qty", 80},
		{"size", bson.D{
			{"h", 14},
			{"w", 21},
			{"uom", "cm"},
		}},
		{"status", "A"},
	},
	bson.D{
		{"item", "sketch pad"},
		{"qty", 95},
		{"size", bson.D{
			{"h", 22.85},
			{"w", 30.5},
			{"uom", "cm"},
		}},
		{"status", "A"},
	},
}

result, err := coll.InsertMany(context.Background(), docs)

Update a Document

result, err := coll.UpdateOne(
	context.Background(), // Get the context parameter
	bson.D{ // Set the query criteria to find the document with item=paper
		{"item", "paper"},
	},
	bson.D{ // Set the update expression
		{"$set", bson.D{ // Use the $set operator to set the updated field content
			{"size.uom", "cm"},  // Change the value of size.uom to cm
			{"status", "P"}, // Change the value of status to P
		}},
		{"$currentDate", bson.D{  // Use the $currentDate operator to update the lastModified field value to the current time
			{"lastModified", true},
		}},
	},
)

Match a document based on the query criteria, and then update the content of this document.

Tip: If you are not familiar with MongoDB, please read the MongoDB tutorial.

Update Documents in Bulk

The difference from the UpdateOne function is that UpdateMany updates the matching documents in bulk based on the matching criteria.

result, err := coll.UpdateMany(
	context.Background(),
	bson.D{ // Set the query criteria, qty > 50
		{"qty", bson.D{
			{"$lt", 50},
		}},
	},
	bson.D{ // Set the update content
		{"$set", bson.D{
			{"size.uom", "cm"},
			{"status", "P"},
		}},
		{"$currentDate", bson.D{
			{"lastModified", true},
		}},
	},
)

Replace a Document

Query a specified document based on the query criteria and replace the document content with the specified content.

Tip: Replacing a document refers to the overall replacement, not the partial update of certain field values in the document.

result, err := coll.ReplaceOne(
	context.Background(),
	bson.D{ // Set the query criteria, item=paper
		{"item", "paper"},
	},
	bson.D{ // Set the new document content
		{"item", "paper"},
		{"instock", bson.A{
			bson.D{
				{"warehouse", "A"},
				{"qty", 60},
			},
			bson.D{
				{"warehouse", "B"},
				{"qty", 40},
			},
		}},
	},
)

First, based on the query criteria, find the document with item=paper, and then replace it with the new document content.