-1

I'm newbie for MongoDB and I’ve tried to use collection.Watch() from "go.mongodb.org/mongo-driver/mongo" lib. and code from https://github.com/minhthuy30197/change_stream/blob/master/main.go. Then I build and run it stop immediately.

I’ve tried to run again and again and it also stop running. I’ve switch using between go run main.go and ./testStreams and it still stop running

So this is my edited code.

    clientOptions := options.Client().
        ApplyURI("mongodb://localhost:27017/test")

    client, err := mongo.Connect(context.TODO(), clientOptions)
    if err != nil {
        log.Fatalf("Failed to create the new client: %v", err)
    }

    ctx := context.Background()
    if err := client.Connect(ctx); err != nil {
        log.Printf("Failed to open client connection: %v", err)
    }
    defer client.Disconnect(ctx)

    coll := client.Database("test").Collection("streams")

    var pipeline interface{}

    for {
        cur, err := coll.Watch(ctx, pipeline)
        if err != nil {
            log.Fatalf("Watch error: %v", err)
        }
        defer cur.Close(ctx)
        log.Println(cur)
        for cur.Next(ctx) {
            elem := CSElem{}
            if err := cur.Decode(elem); err != nil {
                log.Fatalf("Decode error: %v", err)
            }
            log.Println(elem)
        }
        if err := cur.Err(); err != nil {
            log.Fatalf("Error detected: %v", err)
        }
    }

When I edit then error appear

2019/08/07 13:46:39 Failed to open client connection: topology is connected or connecting exit status 1

How should I fix??

  • Hello, welcome to StackOverflow. When `err` variable that is returned by `Watch` does not return `nil`, instead of just `return` please output that as `log.Fatal`. If you run the program again, now you should see an error message. Please add that error message to your question. – Wan B. Aug 06 '19 at 23:03
  • I just edit my question and error appear 2019/08/07 13:46:39 Failed to open client connection: topology is connected or connecting exit status 1 – Grace w. Maew Aug 07 '19 at 06:53
  • Great. Now, what version of `mongo-go-driver` are you using ? If you're using v1+, use `client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))` instead of your `mongo.Connect`. Also, could you connect to MongoDB via `mongo` shell from the same machine ? – Wan B. Aug 07 '19 at 22:19
  • My version is v1.1.0, and then I change to your suggest code and run, it start smoothy!!. So I try to add some data in mongo, it has some error like "Decode error: argument to Decode must be a pointer to a type, but got {{} { } { } {ObjectID("000000000000000000000000")}}" and I fixed it with ```cur.Decode(&elem)``` and it run without error anymore!! Thank you very much @WanBachtiar !!! – Grace w. Maew Aug 08 '19 at 03:47
  • You're very welcome. – Wan B. Aug 08 '19 at 04:06

1 Answers1

0

As mentioned on the comment, utilising mongo-go-driver v1+ a new client instance need to be created first before making a connection. For example:

clientOptions := options.Client().ApplyURI("mongodb://localhost:27017/test")
client, err := mongo.NewClient(clientOptions)
if err != nil {log.Fatal(err)}

// Timed out after 10 seconds of trying to connect 
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

err = client.Connect(ctx)
if err != nil { log.Fatal(err)}
Wan B.
  • 18,367
  • 4
  • 54
  • 71