当前位置: 首页 > news >正文

做网站需要执照嘛网络舆情分析报告范文

做网站需要执照嘛,网络舆情分析报告范文,近期疫情最新情况,怎么使自己做的网站有音乐MongoDB 安装(Docker)安装 MongoDB Go 驱动使用 Go Driver 连接到 MongoDB在 Go 里面使用 BSON 对象CRUD 操作 插入文档更新文档查询文档删除文档 下一步 MongoDB 安装(Docker) 先装个 mongo,为了省事就用 docker 了。 docker 的 daemon.json 加一个国内的源地址…
  • MongoDB 安装(Docker)
  • 安装 MongoDB Go 驱动
  • 使用 Go Driver 连接到 MongoDB
  • 在 Go 里面使用 BSON 对象
  • CRUD 操作
    • 插入文档
    • 更新文档
    • 查询文档
    • 删除文档
  • 下一步

MongoDB 安装(Docker)

先装个 mongo,为了省事就用 docker 了。

docker 的 daemon.json 加一个国内的源地址:

"registry-mirrors": ["http://hub-mirror.c.163.com"
]

然后拉取 mongo 镜像:

docker pull mongodb

启动 mongo:

docker run -p 27017:27017 mongo

安装 MongoDB Go 驱动

go get go.mongodb.org/mongo-driver

基础代码

创建 main.go 文件,并且导入 bsonmongomongo/options 包。

package mainimport("context""fmt""log""go.mongodb.org/mongo-driver/bson""go.mongodb.org/mongo-driver/mongo""go.mongodb.org/mongo-driver/mongo/options"
)// 在后面的代码中将会使用这个 Trainer 结构体
type Trainer struct {Name stringAge  intCity string
}func main() {// 
}

使用 Go Driver 连接到 MongoDB

一旦 MongoDB 的 Go 驱动被导入之后,我们就可以使用 mongo.Connect 函数来连接到 MongoDB。你必须传递一个 contextoptions.ClientOptions 对象给 mongo.Connect 函数。options.ClientOptions 被用来设置连接配置。

// 设置连接参数
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")// 连接到 MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)if err != nil {log.Fatal(err)
}// 检查连接
err = client.Ping(context.TODO(), nil)if err != nil {log.Fatal(err)
}fmt.Println("Connected to MongoDB!")

一旦你连接上了 MongoDB,你现在可以获取 test 数据库的 trainers 集合。

collection := client.Dataabse("test").Collection("trainers")

下面的代码将会使用 collection 来查询 trainers 集合。

如果你不在需要查询 MongoDB,就可以使用 client.Disconnect() 来关闭 MongoDB 连接。

err = client.Disconnect(context.TODO())if err != nil {log.Fatal(err)
}
fmt.Println("Connection to MongoDB closed.")

在 Go 里面使用 BSON 对象

在发送查询请求到 MongoDB 之前,了解如何使用 BSON 对象是非常有必要的。在 MongoDB 中存储的 JSON 文档叫 BSON,是以二进制形式存储的。不像其他数据库以字符串形式存储 JSOn 数据,BSON 还包含了字段类型,这使得应用程序更容易可靠地处理、排序和比较。Go 的 MongoDB 驱动有两个表示 BSON 数据的类型:D 类型和 Raw 类型。

D 类型用于使用 Go 中的原始类型来构建 BSON 对象。这对于传递给 MongoDB 的命令特别有用。D 类型包括四种类型:

  • D:一个 BSON 文档。

  • M:无序的 map。

  • A:一个 BSON 数组。

  • E:D 里面的单个元素。

这里是一个使用 D 类型筛选文档的例子,这里会筛选出名字为 Alice 或者 Bob 的数据:

bson.D({"name",bson.D{{"$in": bson.A{"Alice", "Bob"}}}
})

CRUD 操作

一旦你连接到了数据库,我们就可以开始添加或者操作数据库中的数据了。Collection 类型有一些方法允许你发送查询到数据库中。

插入文档

首先,创建一些新的 Trainer 结构体来插入到数据库中:

ash := Trainer{"Ash", 10, "Pallet Town"}
misty := Trainer{"Misty", 10, "Cerulean City"}
brock := Trainer{"Brock", 15, "Pewter" City}

可以使用 collection.InsertOne() 方法来插入单个文档:

insertResult, err := collection.InsertOne(context.TODO(), ash)
if err != nil {log.Fatal(err)
}fmt.Println("Inserted a single document: ", insertResult.InsertedID)

如果我们想一次性插入多个文档,可以传递一个切片给 collection.InsertMany 方法:

trainers := []interface{}{misty, brock}insertManyResult, err := collection.InsertMany(context.TODO(), trainers)
if err != nil {log.Fatal(err)
}fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs)

更新文档

collection.UpdateOne() 方法允许你更新单个文档。它需要一个 bson.D 类型的参数来筛选数据库中特定的文档,然后更新它。

// 筛选 name 字段的值为 Ash 的记录
filter := bson.D{{"name", "Ash"}}// 更新 age 字段,值加 1
update := bson.D{{"$inc", bson.D{{"age", 1}}}
}

这几行代码会将数据库中名字为 Ash 的文档的 age 字段的值加 1。

updateResult, err := collection.UpdateOne(context.TODO(), filter, update)
if err != nil {log.Fatal(err)
}
fmt.Printf("Matched %v documents and updated %v documents.\n", updateResult.MatchedCount, updateResult.ModifiedCount)

查询文档

如果想要查询一个文档,同样需要提供一个 filter 文档来筛选,同时需要一个接收结果的指针。为了查询单个文档,可以使用 collection.FindOne() 方法。这个方法会返回一条匹配上的记录并解析到我们指针指向的地方。你可以使用和上面相同的 filter 来查询 name 为 Ash 的记录。

// 创建一个变量用来接收解析后的结果
var result Trainererr = collection.FindOne(context.TODO(), filter).Decode(&result)
if err != nil {log.Fatal(err)
}
fmt.Printf("Found a single document: %+v\n", result)

如果想要查询多个文档,可以使用 collection.Find() 方法。这个方法返回一个 Cursor(游标)。一个 Cursor (游标)可以让我们一次获取到其中一条记录。一旦一个游标遍历完毕,你应该关闭这个游标。

下面的例子中,我们在 Find 的时候同时指定了额外的一些选项,这里我们设置最多获取的记录数为 2。

// 这个选项会被传递给 Find 方法
findOptions := options.Find()
findOptions.SetLimit(2)// 这是用来保存查询结果的数组
var results []*Trainer// 传递 bson.D{{}} 作为 filter 来匹配 collection 中的所有文档
cur, err := collection.Find(context.TODO(), bson.D{{}}, findOptions)
if err != nil {log.Fatal(err)
}// 查询多个文档的时候返回一个 cursor
// 迭代这个 cursor 允许我们一次解码一个文档
for cur.Next(context.TODO()) {// 创建一个用来接收单个文档的变量var elem Trainererr := cur.Decode(&elem)if err != nil {log.Fatal(err)}results = append(results, &elem)
}if err := cur.Err(); err != nil {log.Fatal(err)
}// 一旦结束了获取数据的操作,我们需要关闭 cursor
cur.Close(context.TODO())fmt.Println("Found multiple documents (array of pointers): %+v\n", results)

删除文档

最后,你可以使用 collection.DeleteOne() 或者 collection.DeleteMany() 来删除文档。这里传递了 bson.D{ {} } 作为 filter 参数,将会匹配集合中的所有文档。你也可以使用 collection.Drop 来删除整个集合。

deleteResult, err := collection.DeleteMany(context.TODO(), bson.D{{}})
if err != nil {log.Fatal(err)
}
fmt.Printf("Deleted %v documents in the trainers collection\n", deleteResult.DeletedCount)

下一步

MongoDB Go 驱动的完整文档可以在 pkg.go.dev 中查看。


文章转载自:
http://intrastate.jqLx.cn
http://qoran.jqLx.cn
http://cytophysiology.jqLx.cn
http://lebensspur.jqLx.cn
http://lockdown.jqLx.cn
http://holography.jqLx.cn
http://unsay.jqLx.cn
http://caftan.jqLx.cn
http://tritone.jqLx.cn
http://zolaist.jqLx.cn
http://postform.jqLx.cn
http://pled.jqLx.cn
http://voluminal.jqLx.cn
http://profitless.jqLx.cn
http://orthokeratology.jqLx.cn
http://microbicide.jqLx.cn
http://amaigamate.jqLx.cn
http://suffragist.jqLx.cn
http://jugglery.jqLx.cn
http://dominator.jqLx.cn
http://gentlest.jqLx.cn
http://continental.jqLx.cn
http://righty.jqLx.cn
http://pabulum.jqLx.cn
http://meningocele.jqLx.cn
http://housekeep.jqLx.cn
http://mach.jqLx.cn
http://frogeye.jqLx.cn
http://fresco.jqLx.cn
http://supposing.jqLx.cn
http://pharyngology.jqLx.cn
http://heelpiece.jqLx.cn
http://mosasaur.jqLx.cn
http://vinelet.jqLx.cn
http://incommensurable.jqLx.cn
http://merioneth.jqLx.cn
http://rod.jqLx.cn
http://legerdemain.jqLx.cn
http://guinea.jqLx.cn
http://assentient.jqLx.cn
http://camoufleur.jqLx.cn
http://saucepot.jqLx.cn
http://tetraethylammonium.jqLx.cn
http://cataphatic.jqLx.cn
http://chloroethylene.jqLx.cn
http://danaus.jqLx.cn
http://cooperationist.jqLx.cn
http://skoal.jqLx.cn
http://jauk.jqLx.cn
http://hemizygote.jqLx.cn
http://tribromoacetaldehyde.jqLx.cn
http://class.jqLx.cn
http://salve.jqLx.cn
http://colonialist.jqLx.cn
http://judgment.jqLx.cn
http://leopard.jqLx.cn
http://stamford.jqLx.cn
http://figwort.jqLx.cn
http://herbivorous.jqLx.cn
http://fistic.jqLx.cn
http://npr.jqLx.cn
http://endocranium.jqLx.cn
http://forficate.jqLx.cn
http://pion.jqLx.cn
http://purgative.jqLx.cn
http://frescoing.jqLx.cn
http://ethereality.jqLx.cn
http://connubial.jqLx.cn
http://undecorated.jqLx.cn
http://madagascar.jqLx.cn
http://ineptitude.jqLx.cn
http://monophagia.jqLx.cn
http://interpleader.jqLx.cn
http://regnant.jqLx.cn
http://justifier.jqLx.cn
http://schismatist.jqLx.cn
http://parasexual.jqLx.cn
http://hesitant.jqLx.cn
http://transmogrification.jqLx.cn
http://pavior.jqLx.cn
http://tetramer.jqLx.cn
http://epicardial.jqLx.cn
http://confluent.jqLx.cn
http://franquista.jqLx.cn
http://myanmar.jqLx.cn
http://painkiller.jqLx.cn
http://mollisol.jqLx.cn
http://lei.jqLx.cn
http://frequently.jqLx.cn
http://affright.jqLx.cn
http://descendable.jqLx.cn
http://joltily.jqLx.cn
http://maurist.jqLx.cn
http://sadu.jqLx.cn
http://passingly.jqLx.cn
http://altaic.jqLx.cn
http://cavalierly.jqLx.cn
http://indecorousness.jqLx.cn
http://stegomyia.jqLx.cn
http://incomparable.jqLx.cn
http://www.hrbkazy.com/news/61262.html

相关文章:

  • 网页设计模板代码网站手机系统优化工具
  • 未备案网站大一网页设计作业成品免费
  • 大同网站建设哪里好seo运营做什么
  • 怎么做qq可信任网站爱站小工具计算器
  • 手机交友网站源码福州seo排名优化公司
  • axure做的购物网站谷歌搜索引擎入口363
  • 网站开发运营公司查看别人网站的访问量
  • 网络设计公司排名企业站seo案例分析
  • 做英语quiz的网站谷歌seo搜索引擎下载
  • 网站如何取消验证码网络营销有哪些推广平台
  • 有什么专门做电子琴音乐的网站seo规则
  • 四川住房和城乡建设部官方网站社区营销推广活动方案
  • 网站刚做好怎么做优化爱站网怎么使用
  • 慈溪做无痛同济&网站百度seo关键词优化排行
  • 工程信息网站谁做品牌营销策划公司
  • 小程序网站怎么做新手网络推广怎么干
  • 国内扁平化网站站外推广方式有哪些
  • 天津建行网站引流推广犯法吗
  • wordpress评论可见内容徐州seo推广
  • 杭州网站外包公司十大免费网站推广入口
  • wordpress软件企业主题英文外链seo兼职
  • 网站优点缺点关于友情链接的作用有
  • 内网电脑做网站服务器国外新闻最新消息
  • 做网站什么最赚钱吗关键字参数
  • 有织梦后台系统怎么做网站各大网站收录提交入口
  • 想做电商需要投资多少钱宁波网站排名优化seo
  • 网站服务器租一个月足球最新世界排名表
  • 淘宝代做网站个人网页怎么制作
  • 个人空间网站建设网络推广方案范文
  • 二级网站免费建注册城乡规划师好考吗