下了最新版的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.
PHP Driver | MongoDB 2.4 | MongoDB 2.6 | MongoDB 3.0 | MongoDB 3.2 |
---|---|---|---|---|
PHPLIB 1.0 + mongodb-1.1 | ✓ | ✓ | ✓ | ✓ |
mongodb-1.1 | ✓ | ✓ | ✓ | ✓ |
mongodb-1.0 | ✓ | ✓ | ✓ | |
mongo-1.6 | ✓ | ✓ | ✓ | |
mongo-1.5 | ✓ | ✓ | ||
mongo-1.4 | ✓ | ✓ | ||
mongo-1.3 | ✓ |
The driver does not support older versions of MongoDB.
|
既然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 main
import (
"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 [email protected]
*/
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"`
}
/**
注意 索引要設置成 2dsphere
db.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->collection
c := 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)
}
}