当前位置:网站首页>golang使用mongo-driver操作——查(进阶)
golang使用mongo-driver操作——查(进阶)
2022-06-27 21:10:00 【lsjweiyi】
更复杂的查询一时也想不到什么场合用得着,所以以后有了再慢慢补充。
and 和or组合:
// 展示复杂的and和or关系组合查询
func AdvanceAndOr(mongo *mongo.Database, ctx context.Context) {
// 平时我们的条件都是具有隐式的and关系,但是因为同时需要使用两次$or,所以就需要显示使用and
filter := bson.M{
"$and": bson.A{
bson.M{
"$or": bson.A{
bson.M{
"int32": math.MaxInt32}, bson.M{
"int32": math.MaxInt32 - 1}}}, bson.M{
"$or": bson.A{
bson.M{
"boolean": true}, bson.M{
"boolean": false}}}}}
// 查询数据,并解析成结构体
cur, _ := mongo.Collection("test").Find(ctx, filter)
// 遍历数据
for cur.TryNext(ctx) {
result, _ := cur.Current.Elements()
fmt.Println(result)
}
cur.Close(ctx)
}
我认为复杂的查询往往是组合各种关键字进行查询,所以,下面将一些还未实践过的关键字给列出来,方便以后查询。
| 关键字 | 解释 | 链接 |
|---|---|---|
| ne | 不等于 | 链接 |
| not | 反转结果,除了匹配条件以外的所有文档 | 链接 |
| nor | 返回每个条件都不匹配或者就没有该字段的文档,和上面这个很像,但是有区别 | 链接 |
| exists | 具有指定字段的文档 | 链接 |
| type | 匹配字段类型 | 链接 |
| expr | 聚合表达式,功能很强大,很复杂 | 链接 |
| jsonSchema | 文档校验规则,文档的数据本身没有限制,用这个可以限制 | 链接 |
| mod | 求模,用余数做查询条件 | 链接 |
| regex | 正则表达式匹配 | 链接 |
| text文本搜索,但是需要建立索引,不实际 | 链接 |
边栏推荐
- [从零开始学习FPGA编程-48]:视野篇 - 智能传感器的发展与应用
- Feign通过自定义注解实现路径的转义
- NDSS 2022 接收的列表
- Discuz taobaoke website template / Dean taobaoke shopping style commercial version template
- Advertising is too "wild", Yoshino "surrenders"
- 6G显卡显存不足出现CUDA Error:out of memory解决办法
- Halcon's region: features of multiple regions (6)
- [network] common request methods
- go日志包 log的使用
- Small chip chiplet Technology
猜你喜欢

Detect objects and transfer images through mqtt

跨系统数据一致性问题解决方案汇总

Introduction to quantitative trading

seata

c语言字符指针、字符串初始化问题

vivado VIO IP的用法

To build a "strong core" in Nansha, the first IC Nansha conference was held in Nansha

通过 MQTT 检测对象和传输图像

Excel print settings public header

Structure de stockage des graphiques
随机推荐
webService
支持删除,更新任意结点的优先级队列
[network] common request methods
第 2 章 集成 MP
手把手教你移植 tinyriscv 到FPGA上
SQL Server 2016详细安装教程(附注册码和资源)
fiddler 监听不到接口怎么办
企业架构师面试的100个问题
Fsnotify interface of go language to monitor file modification
[Blue Bridge Cup training 100 questions] scratch digital calculation Blue Bridge Cup competition special prediction programming question collective training simulation exercise question No. 16
【AI应用】Jetson Xavier NX的详情参数
SQL中IS NOT NULL与!=NULL的区别
matlab axis坐标轴相关设置详解
Applet referer
halcon之区域:多种区域(Region)特征(6)
如何设置企业微信群机器人定时发消息?
The latest cloud development wechat balance charger special effect applet source code
良/恶性乳腺肿瘤预测(逻辑回归分类器)
Is it safe to use flush mobile phones to speculate in stocks?
实践torch.fx:基于Pytorch的模型优化量化神器