func(mgo *mgo) Start() { collection := mgo.client.Database(DBName).Collection(CollectionName)
collection.Indexes().CreateOne(context.TODO(), mongo.IndexModel{ Keys: bson.M{Key: "2dsphere"}, })
a := Point{"王二", 18, "杭州", Location{"Point", []float64{120.185614,30.300738}}} b := Point{"张三", 25, "杭州", Location{"Point", []float64{120.094778,30.310217}}} c := Point{"小晴", 35, "绍兴", Location{"Point", []float64{120.603847,30.054237}}} d := Point{"李四", 34, "杭州", Location{"Point", []float64{120.110893,30.207849}}} e := Point{"小明", 24, "北京", Location{"Point", []float64{116.435721,39.914031}}} f := Point{"吴六", 25, "杭州", Location{"Point", []float64{120.126443,30.33084}}} h := Point{"于一", 23, "杭州", Location{"Point", []float64{120.28132,30.184083}}} j := Point{"小七", 14, "杭州", Location{"Point", []float64{119.73926,30.247639}}}
insertResult, err := collection.InsertOne(context.TODO(), a) if err != nil { log.Fatal(err) } fmt.Println("Inserted a single document: ", insertResult.InsertedID)
ps := []interface{}{b, c, d, e, f, h, j}
insertManyResult, err := collection.InsertMany(context.TODO(), ps) if err != nil { log.Fatal(err) } fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs) }
|