Skip to content

A Golang Script to read the data from file and update it in mongo database.

Notifications You must be signed in to change notification settings

kashifkhan0771/go-mongo-update-script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Mongo Update Script

This script read data from file and update it in mongo database

The readJSONFile() function reads the data from the JSON file and returns the records in the file as a slice of Record structs.

The connect() function establishes a connection to the MongoDB database and returns a mongo.Client, context.Context, context.CancelFunc, and an error. The ping() function is used to check if the connection is successful.

The findAndValidate() function is used to find documents based on a query and validate if the total documents filtered are greater than the maximum limit. The maximum limit is set as a constant MaxMatchLimit.

The updateOne() function is used to update a single document that matches the filter with the provided update.

In the main() function, the program reads the JSON file using the readJSONFile() function and establishes a connection to the database using connect() function. It then loops through each record in the file and updates the corresponding document in the collection using the updateOne() function. The findAndValidate() function is used to validate if the document should be updated. The total matched and modified count is calculated and printed at the end.

Contact the author

Email: kashifkhan_9@yahoo.com

About

A Golang Script to read the data from file and update it in mongo database.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages