mongoimport

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2023 License: MIT Imports: 9 Imported by: 0

README

mongo-import go package

This package is meant to be used to convert data from a csv/json file and write it into a mongodb collection

mongo-import tools

  • csvreader: Read csv records & entries from a csv file
  • csvimport: convert data from csv and write into a new mongodb collection
  • jsonreader: Read json entries from a json file
  • jsonimport: convert data from json file and write into a new mongodb collection

overview

-Install -Example -License

Installation

Install via go get

Please ensure you have installed Go v1.21 or later

go get github.com/stackbilly/mongo-import

From source code

git clone https://github.com/stackbilly/mongo-import.git
cd mongo-import

Example

package main

import (
	"fmt"
	"log"
	"time"

	mongoimport "github.com/stackbilly/mongo-import" //import package
	"gopkg.in/mgo.v2"
)

func main() {
	records, err := mongoimport.CSVReader("sample.csv") //read csv file records
	if err != nil {
		log.Printf("err: %v", err)
		return
	}
	session, err := mgo.Dial("localhost") //start mongodb session
	if err != nil {
		log.Printf("mongo err: %v", err)
		return
	}
	defer session.Close()
	session.SetMode(mgo.Monotonic, true)
	coll := session.DB("test").C("example-coll")
	start := time.Now()
    //write records to mongodb collection
	count := mongoimport.CSVImport(coll, records, 1, len(records)) 

	fmt.Printf("Inserted %d docs in %v seconds", count, time.Since(start).Seconds())

	// Inserted 1338 docs in 0.3729399 seconds
}

License

This project is licensed under the MIT License - see the LICENSE file for details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CSVImport

func CSVImport(collection *mgo.Collection, records [][]string, start, end int) int

CSVImport func to import csv entries to mongodb

func CSVReader

func CSVReader(filename string) ([][]string, error)

CSVReader func for csv file reader

func JSONFileReader

func JSONFileReader(filename string) (map[string]interface{}, error)

JSONFileReader function json file reader

func JSONImport

func JSONImport(collection *mgo.Collection, contents map[string]interface{}) (int, error)

JSONImport function import/write json contents into a mongodb collection

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL