Implementation of Bulk Update with mongo-go-driver

Overview

Most RDBs have a Bulk Update feature, but MongoDB also has that feature. As you can see in the Documentation here, you can execute update queries all at once. This time, I will introduce an example of executing an update query in Bulk format in Golang.

Correspondence

As mentioned in the Golang. MongoDB bulkWrite () to update slice of documents article, the mongo-go-driver collection provides a method for BulkWrite. Set the query defined in the array of mongo.WriteModel in the argument of BulkWrite and execute it.

Implementation sample

As an example, bulk execute an array of structs that holds the _id to be updated and the status after the update. The implementation of the connection to get the collection object is omitted.

sampleBulkUpdate.go


func SetStatusPosts(request []postModel.PostStatusUpdateRequest) error {
	var operations []mongo.WriteModel
	//Create a query object for update for the number of received arrays
	for _, r := range request {
		operation := mongo.NewUpdateOneModel()
		operation.SetFilter(bson.D{{Key: "_id", Value: r.PostID}})
		operation.SetUpdate(bson.D{{Key: "$set", Value: bson.D{{Key: "status", Value: r.Status}}}})
		operation.SetUpsert(false)
		operations = append(operations, operation)
	}
	bulkOption := options.BulkWriteOptions{}
	bulkOption.SetOrdered(true)
	//Execute Bulk Update. The collection acquisition process is omitted.
	_, err := collection.BulkWrite(context.Background(), operations, &bulkOption)
	if err != nil {
		return nil, err
	}
	return err
}

Recommended Posts

Implementation of Bulk Update with mongo-go-driver
Bulk update with pip (with confirmation)
Bulk update of pip packages
Implementation of Dijkstra's algorithm with python
Clogged with python update of GCP console ①
Clogged with python update of GCP console ② (Solution)
Implementation of TRIE tree with Python and LOUDS
Play with the UI implementation of Pythonista3 [Super Super Introduction]
Sequential update of covariance to derivation and implementation of expressions
Implementation of Fibonacci sequence
Bulk indent with Xcode
Derivation and implementation of update equations for non-negative tensor factorization
Save the output of GAN one by one ~ With the implementation of GAN by PyTorch ~
Easy implementation of credit card payment function with PAY.JP [Django]
How to do Bulk Update with PyMySQL and notes [Python]
[With simple explanation] Scratch implementation of deep Boltzmann machine with Python ②
[With simple explanation] Scratch implementation of deep Boltzmann machine with Python ①
Quantum computer implementation of quantum walk 2
Use "$ in" operator with mongo-go-driver
CNN implementation with just numpy
Implementation of TF-IDF using gensim
Implementation of MathJax on Sphinx
Ensemble learning summary! !! (With implementation)
Equation of motion with sympy
Automatic update of Python module
Explanation and implementation of SocialFoceModel
How to update with SQLAlchemy?
Implementation of game theory-Prisoner's dilemma-
Parallel processing with Parallel of scikit-learn
Implementation of independent component analysis
Prediction of Nikkei 225 with Pytorch 2
Quantum computer implementation of quantum walk 3
Update submodule foreach with GitPython
Python implementation of particle filters
Memories of fighting with Selenium
Prediction of Nikkei 225 with Pytorch
Implementation of quicksort in Python
Quantum computer implementation of quantum walk 1
Deep reinforcement learning 2 Implementation of reinforcement learning
Implementation of Scale-space for SIFT
Implementation of CRUD using REST API with Python + Django Rest framework + igGrid
Crawling with Python and Twitter API 2-Implementation of user search function
Explain how to use TensorFlow 2.X with implementation of VGG16 / ResNet50