Skip to content

Commit 5efff9a

Browse files
author
hero
committed
添加mgo
1 parent 67876a7 commit 5efff9a

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

geo/mgo/main.go

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,71 @@
1-
package mgo
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"go.mongodb.org/mongo-driver/mongo"
7+
"go.mongodb.org/mongo-driver/mongo/options"
8+
"go.mongodb.org/mongo-driver/mongo/readpref"
9+
"go.mongodb.org/mongo-driver/x/bsonx"
10+
)
11+
12+
type User struct {
13+
Name string
14+
Location Location
15+
}
16+
type Location struct {
17+
Longitude float64
18+
Latitude float64
19+
}
20+
21+
func main() {
22+
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://192.168.2.28:27017"))
23+
if err != nil {
24+
panic(err)
25+
}
26+
err = client.Ping(context.Background(), readpref.Primary())
27+
if err != nil {
28+
panic(err)
29+
}
30+
fmt.Println("mgo server success", " ---- > mongodb://192.168.2.28:27017")
31+
dataBase := client.Database("test")
32+
userCollection := dataBase.Collection("user")
33+
/* data := make([]interface{}, 7)
34+
data[0] = User{Name: "天府广场", Location: Location{
35+
Longitude: 104.072833,
36+
Latitude: 30.663422,
37+
}}
38+
data[1] = User{Name: "四川大剧院", Location: Location{
39+
Longitude: 104.074378,
40+
Latitude: 30.664804,
41+
}}
42+
data[2] = User{Name: "新华文轩", Location: Location{
43+
Longitude: 104.070084,
44+
Latitude: 30.664649,
45+
}}
46+
data[3] = User{Name: "手工茶", Location: Location{
47+
Longitude: 104.072402,
48+
Latitude: 30.664121,
49+
}}
50+
data[4] = User{Name: "宽窄巷子", Location: Location{
51+
Longitude: 104.059826,
52+
Latitude: 30.669883,
53+
}}
54+
data[5] = User{Name: "奶茶", Location: Location{
55+
Longitude: 104.06085,
56+
Latitude: 30.670054,
57+
}}
58+
data[6] = User{Name: "钓鱼台", Location: Location{
59+
Longitude: 104.058424,
60+
Latitude: 30.670737,
61+
}}
62+
insertMany, err := userCollection.InsertMany(context.Background(), data)
63+
if err != nil {
64+
panic(err)
65+
}
66+
fmt.Printf("InsertMany插入的消息ID:%v\n", insertMany.InsertedIDs)*/
67+
userCollection.Indexes().CreateOne(context.Background(), mongo.IndexModel{
68+
Keys: bsonx.Doc{{"location", bsonx.String("2d")}},
69+
})
70+
//userCollection.Indexes().DropOne(context.Background(), "location_2d")
71+
}

0 commit comments

Comments
 (0)