Hello everybody,
I try to check if a collection exists to know if is the first execution
First, I connect with MongoDBAtlas successfully:
cliente_local, err := mongo.NewClient(
options.Client().ApplyURI(
useful.CadenaConexion))
useful.Check(fmt.Errorf("[-]It cannot create Newclient", err))
ctx, cancelar = context.WithTimeout(context.Background(), 10*time.Second)
err = cliente_local.Connect(ctx)
if err != nil {
useful.Check(fmt.Errorf("[-] It cannot create Connect", err))
}
defer cancelar()
// Check the connection
err = cliente_local.Ping(ctx, nil)
useful.Check(fmt.Errorf("[-]It cannot Ping", err))
log.Println("[+]Connected to MongoDB Atlas")
After I use a function to check if a collection exists:
var coll *mongo.Collection = mongo_cliente.Collection("config")
exists = true
// Find the document for which the _id field matches id.
// Specify the Sort option to sort the documents by age.
// The first document in the sorted order will be returned.
opts := options.FindOne()
var result bson.M
filter := bson.D{{Key: "first_run", Value: true}}
err := coll.FindOne(
context.TODO(),
filter,
opts,
).Decode(&result)
** Is there another way to check if a collection exists, anyway, with my code does not work **
Thanks in advance!!