MongoDB Golang driver installation + First call to MONGO Command.

Source: Internet
Author: User
Tags pear
This is a creation in Article, where the information may have evolved or changed.

Under the latest version of the Mongodb3.2, under the MongoDB PHP driver.

PHP version is php7,php operation MongoDB class file, the results of the MONGO class does not exist, do not know whether the driver download is not the latest version of the matter.



Crossing network: 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 a 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 Ve rsion: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:p HP 5.4.0 or newer
PEAR package: pear 1.4.8 or newer
DEP Endencies for older releases can is found on the release overview page.

There seems to be a temporary driver for PHP7 's MongoDB ...



Since PHP does not, then use Golang, with Golang to do a Web API bar, anyway recently golang hot.

Search under MongoDB Golang driver, many people recommend MgO, then with the public bar, to GitHub on the search of MgO, installation download.

GitHub Address: Https://github.com/go-mgo/mgo

Website address: Http://labix.org/mgo

Installation address:go get gopkg.in/mgo.v2

API Document Address: HTTPS://GODOC.ORG/GOPKG.IN/MGO.V2

Demo available on the website:

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 8116 9639"},               &person{"Cla", "+55" 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)}

Simple plus easy, install run demo, finished.


The document address of the MongoDB command:

https://docs.mongodb.com/manual/reference/command/



The function to be implemented is to insert a certain number of latitude and longitude coordinate points and then query the coordinate points within a certain distance according to the specified latitude and longitude.

Last chestnut:

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]floa T32 ' Bson: "coordinates" '}type point struct {loc LocS ' Bson: "loc" ' Name string ' Bson: ' name ' '}/** note Index to be set to 2DSPHEREDB.LBSP Lace.ensureindex ({loc: "2dsphere"}) *///receives the coordinate point sent in the Web Form//is temporarily used as a fixed value. Add the coordinate point func addpoint (w http. Responsewriter, R *http. Request) {//parse parameter, default is r not resolved. Parseform () loc: = locs{"point", [2]float32{12.33, 44.55}}point: = Point{loc, "One shop"}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)}}//receives the coordinate point sent in the Web Form//is temporarily used as a fixed value. Near the query, print the result of the query 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")//Read the next source, the return value must be reflect. Ptr or reflect. MAP, or return error or nil. Just such a return value, wasted me for a long time, thought it was the command to write a problem, ... Documents written in the official website are not very detailed. 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 () {//sets the routed HTTP for access. Handlefunc ("/", SayHello) http. Handlefunc ("/addpoint", Addpoint) http. Handlefunc ("/showpoint", Showpoint)//Set the listening port err: = http. Listenandserve (": 9090", nil) if err! = Nil {log. Fatal ("Listenandserve:", Err)}}






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.