Docs Menu

Docs HomeGo

Find Multiple Documents

You can find multiple documents in a collection by using the Find() method.

Tip

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

The following example matches documents in the zips collection in which the pop is less than or equal to 500 people, returning all documents matched:

coll := client.Database("sample_training").Collection("zips")
filter := bson.D{{"pop", bson.D{{"$lte", 500}}}}
cursor, err := coll.Find(context.TODO(), filter)
if err != nil {
panic(err)
}

View a fully runnable example

After you run the full example, it creates a Cursor object that returns the following documents:

// results truncated
...
{ ... , "city" : "SHAKTOOLIK", ... , "pop" : 183, "state" : "AK" },
{ ... , "city" : "DARLINGTON", ... , "pop" : 12, "state" : "ID" },
{ ... , "city" : "EAST BARRE", ... , "pop" : 381, "state" : "VT" },
...

To learn more about specifying query filters and handling potential errors, see Retrieve Data.

To learn more about query operators, see the MongoDB query operator reference documentation.

←  Find a DocumentWrite Operations →
Give Feedback
© 2022 MongoDB, Inc.

About

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