Databases and Collections
Overview
In this guide, you can learn how to interact with MongoDB databases and collections by using the Go driver.
MongoDB organizes data into a hierarchy of the following levels:
Databases: Top-level data structures in a MongoDB deployment that store collections.
Collections: Groups of MongoDB documents. They are analogous to tables in relational databases.
Documents: Units that store literal data such as strings, numbers, dates, and other embedded documents. For more information about document field types and structure, see the Documents entry in the MongoDB Server manual.
Access a Database
You can access a database by using the Database()
method on a Client
instance.
The following example accesses a database named test_database
:
database := client.Database("test_database")
Access a Collection
You can access a collection by using the Collection()
method on a
Database
instance.
The following example accesses a collection named test_collection
:
collection := database.Collection("test_collection")
Tip
If the provided collection name does not already exist in the database, MongoDB implicitly creates the collection when you first insert data into it.
Create a Collection
To explicity create a collection, you can use the CreateCollection()
method
on a Database
instance.
The following example creates a collection named example_collection
:
err = database.CreateCollection(context.TODO(), "example_collection") if err != nil { log.Fatalf("Failed to create collection: %v", err) }
You can specify collection options, such as maximum size and document validation
rules, by passing an options.CreateCollectionOptions
struct to the
CreateCollection()
method of a Database
instance. For a full list of
optional parameters, see the create command entry in the MongoDB Server manual.
Get a List of Collections
You can query for a list of collections in a database by using the
ListCollections()
method on a Database
instance.
The following example gets a list of all the collections in a database, and then prints the collection names to the console:
cursor, err := database.ListCollections(context.TODO(), bson.D{}) if err != nil { log.Fatalf("Failed to list collections: %v", err) } defer cursor.Close(context.TODO()) for cursor.Next(context.TODO()) { var collectionInfo bson.M if err := cursor.Decode(&collectionInfo); err != nil { log.Fatalf("Failed to decode collection info: %v", err) } if name, ok := collectionInfo["name"].(string); ok { fmt.Println(name) } else { log.Println("Collection name not found or not a string") } } if err := cursor.Err(); err != nil { log.Fatalf("Cursor error: %v", err) }
Delete a Collection
You can delete a collection by using the Drop()
method on a Database
instance.
The following example deletes a collection named
test_collection
:
err = database.Collection("test_collection").Drop(context.TODO()) if err != nil { log.Fatalf("Failed to drop collection: %v", err) }
Warning
Dropping a Collection Deletes All Data in the Collection
Dropping a collection from your database permanently deletes all documents and all indexes within that collection.
Drop a collection only if you no longer need the data in that collection.
API Documentation
To learn more about any of the types or methods discussed in this guide, see the following API documentation: