Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
檀越@新空间
Coding Tree
提交
7ca544ad
C
Coding Tree
项目概览
檀越@新空间
/
Coding Tree
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Coding Tree
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
7ca544ad
编写于
10月 14, 2022
作者:
彭世瑜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix
上级
31b672c5
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
136 addition
and
2 deletion
+136
-2
blog/golang/golang-mongo.md
blog/golang/golang-mongo.md
+127
-0
blog/golang/index.md
blog/golang/index.md
+2
-1
weekly/20221017.md
weekly/20221017.md
+7
-1
未找到文件。
blog/golang/golang-mongo.md
浏览文件 @
7ca544ad
...
@@ -175,3 +175,130 @@ func main() {
...
@@ -175,3 +175,130 @@ func main() {
}
}
```
```
## 查找文档
```
go
package
main
import
(
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func
main
()
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
30
)
defer
cancel
()
// 连接到mongo
db_url
:=
"mongodb://localhost:27017"
clientOptions
:=
options
.
Client
()
.
ApplyURI
(
db_url
)
client
,
_
:=
mongo
.
Connect
(
ctx
,
clientOptions
)
defer
client
.
Disconnect
(
ctx
)
// 查询数据
collection
:=
client
.
Database
(
"go_db"
)
.
Collection
(
"student"
)
// 查询所有
// cursor, _ := collection.Find(ctx, bson.D{})
// 增加查询条件 name = "Tom"
cursor
,
_
:=
collection
.
Find
(
ctx
,
bson
.
D
{{
"name"
,
"Tom"
}})
defer
cursor
.
Close
(
ctx
)
// 遍历查询结果
for
cursor
.
Next
(
ctx
)
{
var
result
bson
.
D
cursor
.
Decode
(
&
result
)
fmt
.
Printf
(
"result: %v
\n
"
,
result
)
// result: [{_id ObjectID("634822c35881b85ab2aa138e")} {name Tom} {age 23}]
}
}
```
## 更新文档
```
go
package
main
import
(
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func
main
()
{
ctx
:=
context
.
TODO
()
// 连接到mongo
db_url
:=
"mongodb://localhost:27017"
clientOptions
:=
options
.
Client
()
.
ApplyURI
(
db_url
)
client
,
_
:=
mongo
.
Connect
(
ctx
,
clientOptions
)
defer
client
.
Disconnect
(
ctx
)
// 更新数据
collection
:=
client
.
Database
(
"go_db"
)
.
Collection
(
"student"
)
cursor
,
_
:=
collection
.
UpdateOne
(
ctx
,
bson
.
D
{{
"name"
,
"Tom"
}},
bson
.
D
{{
"$set"
,
bson
.
D
{{
"name"
,
"Tom-1"
},
{
"age"
,
23
}}}},
)
fmt
.
Printf
(
"cursor.ModifiedCount: %v
\n
"
,
cursor
.
ModifiedCount
)
// cursor.ModifiedCount: 1
}
```
## 删除数据
```
go
package
main
import
(
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func
main
()
{
ctx
:=
context
.
TODO
()
// 连接到mongo
db_url
:=
"mongodb://localhost:27017"
clientOptions
:=
options
.
Client
()
.
ApplyURI
(
db_url
)
client
,
_
:=
mongo
.
Connect
(
ctx
,
clientOptions
)
defer
client
.
Disconnect
(
ctx
)
// 删除数据
collection
:=
client
.
Database
(
"go_db"
)
.
Collection
(
"student"
)
cursor
,
_
:=
collection
.
DeleteOne
(
ctx
,
bson
.
D
{{
"name"
,
"Tom"
}},
)
fmt
.
Printf
(
"cursor.DeletedCount: %v
\n
"
,
cursor
.
DeletedCount
)
// cursor.DeletedCount: 1
}
```
\ No newline at end of file
blog/golang/index.md
浏览文件 @
7ca544ad
...
@@ -113,4 +113,5 @@
...
@@ -113,4 +113,5 @@
https://www.bilibili.com/video/BV1ME411Y71o?p=27&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
https://www.bilibili.com/video/BV1ME411Y71o?p=27&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
https://www.bilibili.com/video/BV1zR4y1t7Wj/?p=109&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
https://www.bilibili.com/video/BV1zR4y1t7Wj?p=112&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
weekly/20221017.md
浏览文件 @
7ca544ad
# 全栈爱好者周刊|20221017
# 全栈爱好者
技术
周刊|20221017
## 前端
## 前端
...
@@ -14,8 +14,14 @@
...
@@ -14,8 +14,14 @@
教科书级图解 CSS Grid 布局,收藏了当字典用
教科书级图解 CSS Grid 布局,收藏了当字典用
-
https://mp.weixin.qq.com/s/WNvT3TO6HmlNSEorHwuB4Q
-
https://mp.weixin.qq.com/s/WNvT3TO6HmlNSEorHwuB4Q
Tauri:下一代桌面应用开发框架?
-
https://mp.weixin.qq.com/s/G4K3nIB1SsYDsOxMv7CyFg
## 后端
## 后端
万字长文:带你走进shell世界
万字长文:带你走进shell世界
-
https://mp.weixin.qq.com/s/gtIWO3ItxyLdQeVorXtShQ
-
https://mp.weixin.qq.com/s/gtIWO3ItxyLdQeVorXtShQ
33 个 "不得不看" 的 Python 关键字总结!
-
https://mp.weixin.qq.com/s/yMNyprGNMf4QEjYdG6wAbg
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录