实现目标:根据条件,查询符合条件的mongodb中的记录
func ParkInfoModel(city string, provinnce string, district string) ([]interface{}, error) {
match := bson.M{}
if city != "" {
match["city"] = city
}
if provinnce != "" {
match["provinnce"] = provinnce
}
if district != "" {
match["district"] = district
}
pipeline := []bson.M{
bson.M{"$match": match},
bson.M{"$project": bson.M{"_id": 0}},
}
var data []interface{}
expr := func(c *mgo.Collection) error {
return c.Pipe(pipeline).All(&data)
}
err := mongo.WithMongoCollection(conn, expr)
if err != nil {
return nil, err
}
return data, nil
}