duanpa1980 2016-12-01 23:17
浏览 77

如何按距地理位置的距离和mongodb中的上次激活时间排序?

I have a sore locator app which will display all stores which are in 300 meters of provided latitude and longitude value . The store document contains active time and geo location .

I want to first fetch all stores and then lets say there are 4 stores at 200 meters . So these 4 stores get sorted on the basis of their last active time.

basically what in mysql will look like is order by distance asc , last_active desc

my go code looks like this :

package main

import (
    "encoding/json"
    "fmt"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
    "log"
)

type Store struct {
    ID       string  `bson:"_id,omitempty" json:"shopid"`
    Name     string  `bson:"name" json:"name"`
    Location GeoJson `bson:"location" json:"location"`
    Time    time.Time `bson:"time" json:"time"`
}

type GeoJson struct {
    Type        string    `json:"-"`
    Coordinates []float64 `json:"coordinates"`
}

func main() {
    cluster := "localhost" // mongodb host

    // connect to mongo
    session, err := mgo.Dial(cluster)
    if err != nil {
        log.Fatal("could not connect to db: ", err)
        panic(err)
    }
    defer session.Close()
    session.SetMode(mgo.Monotonic, true)

    // search criteria
    long := 39.6910592
    lat := 35.6909623
    scope := 3000 // max distance in metres

    var results []Store // to hold the results

    // query the database
    c := session.DB("test").C("stores")

    // insert
    man := Store{}
    man.ID = "1"
    man.Name = "vinka medical store"
    man.Location.Type = "Point"
    man.Location.Coordinates = []float64{lat, long}

    // insert
    err = c.Insert(man)
    fmt.Printf("%v
", man)
    if err != nil {
        fmt.Println("There is insert error")
        panic(err)
    }

    // ensure
    // Creating the indexes
    index := mgo.Index{
        Key:  []string{"$2dsphere:location"},
        Bits: 26,
    }
    err = c.EnsureIndex(index)
    if err != nil {
        fmt.Println("There is index error")
        panic(err)
    }

    //find
    err = c.Find(bson.M{
        "location": bson.M{
            "$nearSphere": bson.M{
                "$geometry": bson.M{
                    "type":        "Point",
                    "coordinates": []float64{long, lat},
                },
                "$maxDistance": scope,
            },
        },
    }).All(&results)

    //err = c.Find(bson.M{"_id": "1"}).All(&results)
    if err != nil {
        panic(err)
    }

    //convert it to JSON so it can be displayed
    formatter := json.MarshalIndent
    response, err := formatter(results, " ", "   ")

    fmt.Println(string(response))
}

How should i modify this part ?

//find
err = c.Find(bson.M{
    "location": bson.M{
        "$nearSphere": bson.M{
            "$geometry": bson.M{
                "type":        "Point",
                "coordinates": []float64{long, lat},
            },
            "$maxDistance": scope,
        },
    },
}).All(&results) 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?