package main
import (
"fmt"
"log"
)
func main() {
// connect to MongoDB
if err != nil {
log.Fatal(err)
}
// initalize the test collection on the test database
c := session.DB("test").C("test")
// get all the documents in the test collection
var results []bson.D
err = c.Find(bson.M{}).All(&results)
if err != nil {
log.Fatal(err)
}
// iterate over all the docs
for _, doc := range results {
// for each doc make a map for the keys in the doc
keys := make(map[string]bool)
// iterate over each key-value pair in each doc
for _, kv := range doc {
// if a key has been seen before print the doc
if _, ok := keys[kv.Name]; ok {
fmt.Println(doc)
}
// mark that we've seen this key
keys[kv.Name] = true
}
}
}