Skip to content

Instantly share code, notes, and snippets.

@scald
Created December 24, 2014 13:52
Show Gist options
  • Save scald/592d36a2fd7c063e0f1d to your computer and use it in GitHub Desktop.
Save scald/592d36a2fd7c063e0f1d to your computer and use it in GitHub Desktop.
Exports tab delimited name and email from a Meteor users collection
package main
import (
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"log"
"time"
"fmt"
)
// fill in your Mongo connection details here!
const (
MongoDBHosts = ""
AuthDatabase = ""
AuthUserName = ""
AuthPassword = ""
TestDatabase = ""
)
type (
// fields we want from the profile object in mgo
Profile struct {
Name string `bson:"name"`
}
// fields we want from the email address object in mgo
Email struct {
Address string `bson:"address"`
Verified bool `bson:"verified"`
}
User struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Emails []Email `bson:"emails,omitempty"`
Profile Profile `bson:"profile,omitempty"`
}
)
func main() {
// We need this object to establish a session to our MongoDB.
mongoDBDialInfo := &mgo.DialInfo{
Addrs: []string{MongoDBHosts},
Timeout: 60 * time.Second,
Database: AuthDatabase,
Username: AuthUserName,
Password: AuthPassword,
}
// Create a session which maintains a pool of socket connections
// to our MongoDB.
mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
if err != nil {
log.Fatalf("CreateSession: %s\n", err)
}
// Reads may not be entirely up-to-date, but they will always see the
// history of changes moving forward, the data read will be consistent
// across sequential queries in the same session, and modifications made
// within the session will be observed in following queries (read-your-writes).
// http://godoc.org/labix.org/v2/mgo#Session.SetMode
mongoSession.SetMode(mgo.Monotonic, true)
// // Create a wait group to manage the goroutines.
// var waitGroup sync.WaitGroup
// // Perform 10 concurrent queries against the database.
// waitGroup.Add(10)
// for query := 0; query < 10; query++ {
RunQuery(mongoSession)
// }
// Wait for all the queries to complete.
// waitGroup.Wait()
// log.Println("All Queries Completed")
}
// RunQuery is a function that is launched as a goroutine to perform
// the MongoDB work.
func RunQuery(mongoSession *mgo.Session) {
// Request a socket connection from the session to process our query.
// Close the session when the goroutine exits and put the connection back
// into the pool.
sessionCopy := mongoSession.Copy()
// defer sessionCopy.Close()
// Get a collection to execute the query against.
collection := sessionCopy.DB(TestDatabase).C("users")
// Retrieve the list of users.
var users []User
err := collection.Find(nil).All(&users)
if err != nil {
log.Printf("RunQuery : ERROR : %s\n", err)
return
}
// iterate through the users and print out a tab delimited list of name and email
for _,element := range users {
for _,el := range element.Emails {
fmt.Println(element.Profile.Name, "\t", el.Address)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment