MongoDB的golang驅動安裝 + 首次調用Mongo Command。

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

下了最新版的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.



Dependencies
Release 1.1.7: PHP Version: PHP version 7.99.99 or older
PHP Version: PHP 5.4.0 or newer
PEAR Package: PEAR 1.4.8 or newer
Release 1.1.6: PHP Version: PHP version 7.99.99 or older
PHP Version: PHP 5.4.0 or newer
PEAR Package: PEAR 1.4.8 or newer
Release 1.1.5: PHP Version: PHP version 7.99.99 or older
PHP Version: PHP 5.4.0 or newer
PEAR Package: PEAR 1.4.8 or newer
Dependencies for older releases can be found on the release overview page.

好像是暫時還有適合PHP7的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 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)}}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.