I am watching a mongodb
collection with for all insert
events using the golang
. I used the mongodb function, changes streams.
My requirements is to access the data inside that return event where it seems to be of type bson.m
Here's my code sample:
func watch_for_events() {
fmt.Println("Watching Started....")
matchPipeline := bson.D{{"$match", bson.D{{"operationType", "insert"}}}}
// open a change stream with an empty pipeline parameter
coll := mongoConnection.Database(mongo_db).Collection(added_collection)
changeStream, err := coll.Watch(context.TODO(), mongo.Pipeline{matchPipeline})
if err != nil {
panic(err)
}
defer changeStream.Close(context.TODO())
// iterate over the cursor to print the change stream events
for changeStream.Next(context.TODO()) {
fmt.Println(changeStream.Current)
fmt.Printf("POD name is: %v", changeStream.Current.Lookup("pod_name"))
}
fmt.Println("Watching Ended....")
}
This is the output I am getting
map[_id:map[_data:82637B3725000000012B022C0100296E5A10046C554EC75E644A81AF98CC28BAF03C0246645F69640064637B37253BC63551C4856EB50004] clusterTime:{1669019429 1} documentKey:map[_id:ObjectID("637b37253bc63551c4856eb5")] fullDocument:map[_id:ObjectID("637b37253bc63551c4856eb5") containers_and_images:[[nginx] [nginx:stable]] pod_name:nginx-847c4cd46c-dn2sc total_container_count:1] ns:map[coll:app1-added-pods db:ng-db] operationType:insert]
But the line fmt.Printf("POD name is: %v", changeStream.Current.Lookup("pod_name"))
says no element found. I need to get the data such as, pod_name
, namespace
, total_container_count
etc.
Can someone please help me with this?