// // Returns all collections in database // func (d *Resource) DatabaseCollectionsHandler(req *restful.Request, resp *restful.Response) { // Mongo session session, needclose, err := d.SessMng.Get(getParam("alias", req)) //先查服务器 if err != nil { WriteError(err, resp) return } if needclose { defer session.Close() } // Database request parameter dbname := getParam("database", req) //再拿到数据库信息 println(dbname) // Get collections from database collections, err := session.DB(dbname).CollectionNames() //拿到数据库下所有的集合 if err != nil { WriteError(err, resp) return } if collections == nil { err = errors.New("Unknown database: " + dbname) WriteError(err, resp) return } // Write collections back to client WriteResponse(collections, resp) }
// GET http://localhost:8181/stats/local/landskape/systems func (s *Resource) CollectionStatisticsHandler(req *restful.Request, resp *restful.Response) { // Mongo session session, needsClose, err := s.SessMng.Get(req.PathParameter("alias")) if err != nil { WriteError(err, resp) return } if needsClose { defer session.Close() } var ( // Request parameters collection = req.PathParameter("collection") database = req.PathParameter("database") // Statistics result result = bson.M{} ) // Get statistics for collection err = session.DB(database).Run(bson.M{"collStats": collection, "scale": 1}, &result) if err != nil { WriteError(err, resp) return } // Write result to console fmt.Printf("stats result:%#v", result) // Write result back to client WriteResponse(result, resp) }