package main
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
// Set up MongoDB connection parameters
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
log.Fatal(err)
}
// Choose the database and collection
database := client.Database("your_database_name")
collection := database.Collection("your_collection_name")
// Define the filter to find documents without specifying the "_id"
filter := bson.M{} // Empty filter matches all documents
// Execute the find operation
cursor, err := collection.Find(context.Background(), filter)
if err != nil {
log.Fatal(err)
}
defer cursor.Close(context.Background())
// Iterate through the cursor and print the results
for cursor.Next(context.Background()) {
var result bson.M
if err := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
// Check for errors during cursor iteration
if err := cursor.Err(); err != nil {
log.Fatal(err)
}
}