Your first example doesn't work because Collection.Find() expects a single filter document and you pass a slice of documents to it.
A single document may contain multiple filters, e.g. bson.M{"age": bson.M{"$gt": 20}, "name": "Bob"}, so in practice this doesn't pose a restriction.
If you have multiple filter documents and you want to apply all, you have to "merge" them into a single document.
A general solution is to create a document with an $and key (if you want them in logical AND connection, or $or if you want logical OR), whose value in the map is the slice of filters (documents).
This is how easy it is:
// filters contains all the filters you want to apply:
filters := []bson.M{
bson.M{"age": bson.M{"$gt": 20}},
bson.M{"name": "Bob"},
}
// filter is a single filter document that merges all filters
filter := bson.M{"$and": filters}
// And then:
err := coll.Find(filter).All(&persons)
Find()expects a filter document and in your first example you pass a slice of documents. It has nothing to do with being dynamic. A single document may contain multiple filters, e.g.bson.M{"age": bson.M{"$gt": 20}, "name": "Bob"}. What is your question?