error=topology is closed

524 views
Skip to first unread message

Kai Hendry

unread,
May 28, 2018, 3:37:10 AM5/28/18
to mongodb-go-driver
Hi, I'm struggling to get your example from https://godoc.org/github.com/mongodb/mongo-go-driver/mongo
working on go version go1.10.2 linux/amd64. What am I missing? Tbh I don't quite understand all those context.Background() stuff, but I am going along with it.

[hendry@t480s lambda2mongo]$ git --git-dir /home/hendry/go/src/github.com/mongodb/mongo-go-driver/.git describe                                                
v0.0.5-24-gcf7d3d6                     
[hendry@t480s lambda2mongo]$ cat main.go                                       
package main                           

import (                               
        "context"                      

        "github.com/apex/log"          
        "github.com/mongodb/mongo-go-driver/bson"                              
        "github.com/mongodb/mongo-go-driver/mongo"                             
)                                      

func main() {                          
        client, err := mongo.NewClient("mongodb://localhost:27017")            
        if err != nil {                
                log.WithError(err).Fatal("error connecting")                   
        }                              
        collection := client.Database("admin").Collection("system.version")    

        cur, err := collection.Find(context.Background(), nil)                 
        if err != nil {                
                log.WithError(err).Fatal("failed to find collection")          
        }                              

        defer cur.Close(context.Background())                                  
        for cur.Next(context.Background()) {                                   
                elem := bson.NewDocument()                                     
                err := cur.Decode(elem)                                        
                if err != nil {        
                        log.WithError(err).Fatal("failed to decode element")   
                }                      
                // do something with elem....                                  
        }                              
        if err := cur.Err(); err != nil {                                      
                log.WithError(err).Fatal("failed to get cursor")               
        }                              

}                                      
[hendry@t480s lambda2mongo]$ go run main.go                                    
2018/05/28 15:32:18 fatal failed to find collection error=topology is closed   
exit status 1                          
[hendry@t480s lambda2mongo]$ mongo mongodb://localhost:27017                   
MongoDB shell version v3.6.4           
connecting to: mongodb://localhost:27017                                       
MongoDB server version: 3.6.4          
Server has startup warnings:           
2018-05-28T14:37:21.072+0800 I STORAGE  [initandlisten]                        
2018-05-28T14:37:21.072+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine        
2018-05-28T14:37:21.072+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem                                    
2018-05-28T14:37:21.580+0800 I CONTROL  [initandlisten]                        
2018-05-28T14:37:21.580+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.                                            
2018-05-28T14:37:21.580+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.                           
2018-05-28T14:37:21.580+0800 I CONTROL  [initandlisten]                        
> use admin                            
switched to db admin                   
> db.system.version.find()             
{ "_id" : "featureCompatibilityVersion", "version" : "3.6" }                   
>                                      
bye                                    
[hendry@t480s lambda2mongo]$           

Rajiv Subramanian M

unread,
May 28, 2018, 3:41:43 AM5/28/18
to mongodb-...@googlegroups.com
After this lines
client, err := mongo.NewClient("mongodb://localhost:27017")            
if err != nil {
    log.WithError(err).Fatal("error connecting")
}                              

Include
client.Connect(context.Background())


--
You received this message because you are subscribed to the Google Groups "mongodb-go-driver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-go-dri...@googlegroups.com.
To post to this group, send email to mongodb-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-go-driver/bcadc91f-96c0-4813-a9bc-ab93c8d5932d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
~ ☮ ~
Rajiv

Kai Hendry

unread,
May 28, 2018, 4:05:04 AM5/28/18
to mongodb-go-driver
Thanks Rajiv, it now doesn't error out.

But how do I get some output from this cursor { "_id" : "featureCompatibilityVersion", "version" : "3.6" } ?

Are there some complete examples somewhere I could study?               

Rajiv Subramanian M

unread,
May 28, 2018, 5:05:38 AM5/28/18
to mongodb-go-driver
Yes there are examples here: https://github.com/mongodb/mongo-go-driver/tree/master/examples/documentation_examples

To query on collection you do this,

cur, err := collection.Find(
   context.Background(),
   bson.NewDocument(bson.EC.String("_id", "featureCompatibilityVersion"), bson.EC.String("version", "3.6")),
)

Kai Hendry

unread,
May 28, 2018, 5:28:47 AM5/28/18
to mongodb-go-driver
It's not clear how to map JSON to BSON. Are there some examples to do this?

My use case is that I need to update a MongoDB with JSON payloads.


Many thanks,

Rajiv Subramanian M

unread,
May 28, 2018, 5:40:48 AM5/28/18
to mongodb-go-driver
The bson library used in mongo-go-driver is their own implementation of bson. Not sure if any package available do convert json to bson but found this one on stackoverflow https://stackoverflow.com/questions/39785289/how-to-marshal-json-string-to-bson-document-in-golang-for-writing-to-mongodb?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Reply all
Reply to author
Forward
0 new messages