package db import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/sys/mgo" "go_dreamfactory/pb" "math/rand" "sync" "testing" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo/options" ) func Test_Id(t *testing.T) { wg := new(sync.WaitGroup) for i := 0; i < 3; i++ { wg.Add(1) go func(_wg *sync.WaitGroup) { for i := 0; i < 10; i++ { id := primitive.NewObjectID().Hex() fmt.Printf(id + "\n") } _wg.Done() }(wg) } wg.Wait() } func Test_2D(t *testing.T) { if sys, err := mgo.NewSys( mgo.SetMongodbUrl("mongodb://10.0.0.9:10013"), mgo.SetMongodbDatabase("dreamfactory4"), ); err != nil { fmt.Printf("err:%v", err) return } else { port := []int32{2 * 1000, rand.Int31n(100)} fmt.Printf("port:%v \n", port) if cursor, err := sys.Find(comm.TableArena, bson.M{ "dan": 2, "loc": bson.M{ "$geoWithin": bson.M{ "$center": bson.A{port, 30}, }, }, // bson.A{[]int32{2 * 1000, rand.Int31n(100)}, 100} // "loc": bson.M{ // "$near": bson.M{ // "$geometry": bson.M{"type": "Point", "coordinates": []int32{dan * 1000, rand.Int31n(100)}}, // "$maxDistance": 100, // }, // }, }, options.Find().SetSkip(0).SetLimit(int64(5))); err != nil { fmt.Printf("err:%v", err) return } else { for cursor.Next(context.Background()) { temp := &pb.DBArenaUser{} if err = cursor.Decode(temp); err != nil { fmt.Printf("err:%v", err) return } fmt.Printf("temp:%v\n", temp.Uid) } } } }