// A function that Dials and Authenticates // into a provided data base // accespts host,db,user,pass,log=Bool // returns db,err func ConnAndAuth(h, d, u, p string, l bool) mongo.Database { conn, err := mongo.Dial(h) if err != nil { log.Fatal(err) } defer conn.Close() if l { conn = mongo.NewLoggingConn(conn, log.New(os.Stdout, "", 0), "") // clear log prefix for more readable output log.SetFlags(0) } db := mongo.Database{conn, d, mongo.DefaultLastErrorCmd} err = db.Authenticate(u, p) if err != nil { log.Fatal(err) } return db }
func main() { // create file file, err := os.Create("items.csv") if err != nil { panic(err) } defer file.Close() // Create CSV Writer writer := csv.NewWriter(file) // Connect to MongoDB conn, err := mongo.Dial("dharma.mongohq.com:10053") if err != nil { log.Fatal(err) } defer conn.Close() // Wrap the connection with a logger so that we can view traffic // to and from the mongoDB server // conn = mongo.NewLoggingConn(conn, log.New(os.Stdout, "", 0), "") // clear log prefix for more readable output log.SetFlags(0) // Create db object db := mongo.Database{conn, "nix_stagingv3", mongo.DefaultLastErrorCmd} err = db.Authenticate("sync", "vbnvbn45") if err != nil { log.Fatal(err) } // Create collections object items := db.C("items") // Auto Detect Headerline var m mongo.M err = items.Find(nil).One(&m) if err != nil && err != mongo.Done { log.Fatal(err) } // Create a cursor using Find query cursor, err := items.Find(nil).Cursor() if err != nil { log.Fatal(err) } defer cursor.Close() // Iterate over all items in a collection for cursor.HasNext() { var m mongo.M err := cursor.Next(&m) if err != nil { log.Fatal(err) } var record []string var header []string for k, _ := range m { s := fmt.Sprint(v) record = append(record, s) } writer.Write(record) writer.Flush() } }
func main() { // *host host := flag.String("host", "localhost:27017", "The host and port of the mongod instance you wish to connect to") dbname := flag.String("db", "testDB", "The output file that will be written to") user := flag.String("user", "", "The user you wish to authenticate with") pass := flag.String("pass", "", "The pass you wish to authenticate with") col := flag.String("col", "testCol", "The collection you wish to output") // fieldsFile := flag.String("fieldsFile", "~/", "The a file containing field names") // query := flag.String("query", "collection.csv", "The output file that will be written to") out := flag.String("out", "collection.csv", "The output file that will be written to") flag.Parse() // After cmd flag parse file, err := os.Create(*out) if err != nil { log.Fatal(err) } defer file.Close() // Create Writer writer := csv.NewWriter(file) // Connect to MongoDB conn, err := mongo.Dial(*host) if err != nil { log.Fatal(err) } defer conn.Close() // select DB db := mongo.Database{conn, *dbname, mongo.DefaultLastErrorCmd} // test for user and pass vars err = db.Authenticate(*user, *pass) if err != nil { log.Fatal(err) } // select Collection collection := db.C(*col) // Auto Detect Headerline var h mongo.M err = collection.Find(nil).One(&h) if err != nil { log.Fatal(err) } var headers []string for k, _ := range h { headers = append(headers, fmt.Sprint(k)) } // Default sort the headers // Otherwise accessing the headers will be // different each time. sort.Strings(headers) // write headers to file writer.Write(headers) writer.Flush() // log.Print(headers) // Create a cursor using Find query cursor, err := collection.Find(nil).Cursor() if err != nil { log.Fatal(err) } defer cursor.Close() // Iterate over all items in a collection for cursor.HasNext() { var m mongo.M err := cursor.Next(&m) if err != nil { log.Fatal(err) } var record []string for _, header := range headers { record = append(record, fmt.Sprint(m[header])) } writer.Write(record) writer.Flush() } }