• “黑神话·悟空”免安装在线玩,无视显卡等硬件要求!!
  • √ 游戏免下载安装 √ 5分钟随开随玩 √ 关机不计费!低至¥14.90/月!
🔥进入竞技-直面天命🔥

使用golang的mongo-driver库如何插入一条带有lat lng的2dsphere数据,代码注意排版显示

时间2023-03-03 19:55:58 提问来自 山东省青岛市 的网友 分类创作 浏览356
package main import ( "context" "fmt" "go.mongodb.org/mongo-driver/bson/primitive" //引入bson库,用于操作object id //引入mongo driver库,用于连接数据库和插入数据 "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" //引入geo类型的包,用于处理空间数据类型 ) import "go.mongodb.org/mongo-driver/bson"; func main() { // 连接到MongDB服务 clientOptions := options.Client().ApplyURI("mongodb://localhost:27017") client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { fmt.Println(err) return } // 检测连接是否成功 (此处可以省略) dbName为要使用的数据库名称,collectionName为要使用的集合名称 collection := client.Database("dbName").Collection("collectionName") err = client.Ping(context, readpref); if err != nil { fmtPrintln(err) return } // 插入一条带有lat lng的2dsphere数据 insertResult, err := collection .InsertOne(context .TODO(), bson .M{"_id": primitive .NewObjectID (), "name": "test", "loc": bson .M{"type": "Point", "coordinates": []float64{123, 456}}}) if err != nil { fmtPrintln(err) return } fmtPrintln("Inserted a single document: %v\n", insertResult .InsertedID ) }

点击这里,进行内容创作

创作工场 免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!

上一篇 使用golang的mongo-driver库如何插入一条带有lat lng的2dsphere数据 下一篇 神印王座圣采儿被鸡巴插入