MongoDB的golang驱动安装 + 首次调用Mongo Command。
来源:互联网 发布:无根门奇遇知乎 编辑:程序博客网 时间:2024/05/17 04:09
下了最新版的Mongodb3.2,下了mongodb的PHP驱动。
PHP的版本是php7,PHP的操作mongodb的类文件,结果尼玛的,提示Mongo类不存在,不知道是不是驱动下载的不是最新版的事。
看官网:https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-php
https://pecl.php.net/package/mongodb
In the table below, mongodb and mongo refer to the new and legacy MongoDB PHP driver, respectively.PHPLIB refers to the userland library.
The driver does not support older versions of MongoDB.
PHP Version: PHP 5.4.0 or newer
PEAR Package: PEAR 1.4.8 or newer
PHP Version: PHP 5.4.0 or newer
PEAR Package: PEAR 1.4.8 or newer
PHP Version: PHP 5.4.0 or newer
PEAR Package: PEAR 1.4.8 or newerDependencies for older releases can be found on the release overview page.
既然PHP不行,那就用Golang,用golang做个web api吧,反正最近Golang特热门的。
搜索下Mongodb的golang驱动,不少人推荐mgo的,那就随大众吧,去github上搜了mgo,安装下载。
github地址:https://github.com/go-mgo/mgo
官网地址:http://labix.org/mgo
安装地址:go get gopkg.in/mgo.v2
API文档地址:https://godoc.org/gopkg.in/mgo.v2
官网提供的demo:
package mainimport ( "fmt""log" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson")type Person struct { Name string Phone string}func main() { session, err := mgo.Dial("server1.example.com,server2.example.com") if err != nil { panic(err) } defer session.Close() // Optional. Switch the session to a monotonic behavior. session.SetMode(mgo.Monotonic, true) c := session.DB("test").C("people") err = c.Insert(&Person{"Ale", "+55 53 8116 9639"}, &Person{"Cla", "+55 53 8402 8510"}) if err != nil { log.Fatal(err) } result := Person{} err = c.Find(bson.M{"name": "Ale"}).One(&result) if err != nil { log.Fatal(err) } fmt.Println("Phone:", result.Phone)}
简单加easy的,安装跑demo,完毕。
mongodb的Command的文档地址:
https://docs.mongodb.com/manual/reference/command/
大约要实现的功能是,插入一定数量的经纬度坐标点,然后根据根据指定的经纬度查询附近一定距离内的坐标点。
上个栗子:
package main/**@author 908204694@qq.com*/import ("fmt""log""net/http""reflect""strings""gopkg.in/mgo.v2""gopkg.in/mgo.v2/bson")const MONGO_URL = "localhost:27017"type LocS struct {Type string `bson:"type"`Coordinates [2]float32 `bson:"Coordinates"`}type Point struct {Loc LocS `bson:"loc"`Name string `bson:"name"`}/**注意 索引要设置成 2dspheredb.lbsplace.ensureIndex( { loc: "2dsphere" } ) */// 接收web form中送过来的坐标点// 暂时是使用的固定值。// 添加坐标点func addPoint(w http.ResponseWriter, r *http.Request) {// 解析参数, 默认是不会解析的r.ParseForm()loc := LocS{"Point", [2]float32{12.33, 44.55}}point := Point{loc, "一号店"}session, err := mgo.Dial(MONGO_URL)if err != nil {panic(err)}defer session.Close()// Optional. Switch the session to a monotonic behavior.session.SetMode(mgo.Monotonic, true)// database->collectionc := session.DB("lbs").C("lbsplace")err = c.Insert(&point)if err != nil {log.Fatal(err)}}// 接收web form中送过来的坐标点// 暂时是使用的固定值。// 查询附近,打印查询结果func showPoint(w http.ResponseWriter, r *http.Request) {r.ParseForm()session, err := mgo.Dial(MONGO_URL)if err != nil {panic(err)}defer session.Close()session.SetMode(mgo.Monotonic, true)db := session.DB("lbs")// 看了下源码,返回值必须是 reflect.Ptr 或者 reflect.Map ,否则返回error或者nil。// 就这么一个返回值的,浪费我好长时间,以为是command写的有问题的,。。。官网的文档写的不太详细result := bson.M{}errRun := db.Run(bson.D{{"geoNear", "lbsplace"},{"spherical", true},{"near", [2]float64{118.19079, 35.677136}},{"num", 2}},&result)fmt.Println("result:", result, errRun)}func sayHello(w http.ResponseWriter, r *http.Request) {fmt.Fprintf(w, "Hello!\n")}func main() {// 设置访问的路由http.HandleFunc("/", sayHello)http.HandleFunc("/addpoint", addPoint)http.HandleFunc("/showpoint", showPoint)// 设置监听的端口err := http.ListenAndServe(":9090", nil)if err != nil {log.Fatal("ListenAndServe: ", err)}}
- MongoDB的golang驱动安装 + 首次调用Mongo Command。
- Golang MongoDB 驱动 mgo 的若干问题
- mongodb安装和mongo-c-driver的安装使用
- 【mongoDB实战】mongo安装配置
- MongoDB-C驱动的安装
- Golang 中操作 Mongo Update 的方法
- 安装mongodb 配置php mongo扩展
- Windows下安装PHP的MongoDB驱动
- 安装MongoDB的PHP驱动模块扩展
- Windows下安装PHP的MongoDB驱动
- 一、Mongo的安装
- mongo的安装
- mongo的安装
- .NET 操作MongoDB入门(二)安装MongoDB的C#驱动
- linux上安装mongodb扩展——mongo和mongodb
- Mongo Wire Protocol(写MongoDB驱动必备)
- PHP安装MongoDB驱动
- 安装PHP MongoDB驱动
- ajax+php实现自动提示文本框
- 数据库连接池的好处
- 转:前端工程师须知的21个网页前端性能优化
- 黑盒和白盒测试
- IOS Dev Intro - Delegation Notification and Observation
- MongoDB的golang驱动安装 + 首次调用Mongo Command。
- GC的介绍
- [Java] 获取随机数的3种方法
- php 初学
- Service与远程进程通过Messenger进行远程通信(IPC)
- MathType中输入不了汉字怎么办
- 批量删除用户
- java开发经典书籍
- [Java] super,this 的区别及构造方法的使用