实现目标:根据条件,查询符合条件的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
}
MongoDB查询实践
本文介绍了一个用于查询MongoDB数据库中特定记录的Go语言函数实现。该函数可以根据城市、省份和区县等条件筛选数据,并通过管道操作进行字段投影,最终返回查询结果。
2570

被折叠的 条评论
为什么被折叠?



