Docs Menu

Docs HomeGo

Insert Multiple Documents

You can insert multiple documents into a collection by using the InsertMany() method.


Read the Usage Examples to learn how to run this example.

This example uses the following Restaurant struct as a model for documents in the restaurants collection:

type Restaurant struct {
Name string
RestaurantId string `bson:"restaurant_id,omitempty"`
Cuisine string `bson:"cuisine,omitempty"`
Address interface{} `bson:"address,omitempty"`
Borough string `bson:"borough,omitempty"`
Grades []interface{} `bson:"grades,omitempty"`

The omitempty struct tag omits the corresponding field from the inserted document when left empty.

The following example inserts two new documents to the restaurants collection:


Non-existent Databases and Collections

If the necessary database and collection don't exist when you perform a write operation, the server implicitly creates them.

coll := client.Database("sample_restaurants").Collection("restaurants")
newRestaurants := []interface{}{
Restaurant{Name: "Rule of Thirds", Cuisine: "Japanese"},
Restaurant{Name: "Madame Vo", Cuisine: "Vietnamese"},
result, err := coll.InsertMany(context.TODO(), newRestaurants)
if err != nil {

View a fully runnable example

After you run the full example, you can find the following inserted documents in the restaurants collection:

{ "_id": ObjectID("..."), "name": "Rule of Thirds", "cuisine": "Japanese"},
{ "_id": ObjectID("..."), "name": "Madame Vo", "cuisine": "Vietnamese"}

For an example on how to find multiple documents, see the Find Multiple Documents usage example.

To learn more about inserting documents, see inserting documents.


←  Insert a DocumentUpdate a Document →
Share Feedback
© 2023 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.