当前位置:网站首页>Golang uses Mongo driver operation - query (Advanced)
Golang uses Mongo driver operation - query (Advanced)
2022-06-27 23:43:00 【lsjweiyi】
For more complex queries, I can't think of any occasion to use them , So I will add it slowly when I have it .
and and or Combine :
// Show complex and and or Relation combination query
func AdvanceAndOr(mongo *mongo.Database, ctx context.Context) {
// Usually our conditions are implicit and Relationship , But because it needs to be used twice at the same time $or, So it is necessary to display and use 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}}}}}
// Query data , And parse it into a structure
cur, _ := mongo.Collection("test").Find(ctx, filter)
// Traversal data
for cur.TryNext(ctx) {
result, _ := cur.Current.Elements()
fmt.Println(result)
}
cur.Close(ctx)
}
In my opinion, complex queries are often combined with various keywords , therefore , Here are some keywords that have not been practiced , For future reference .
| keyword | explain | link |
|---|---|---|
| ne | It's not equal to | link |
| not | Reverse results , All documents except matching criteria | link |
| nor | Return documents that do not match each condition or do not have this field , It's very similar to the one above , But there's a difference | link |
| exists | Documents with specified fields | link |
| type | Match field type | link |
| expr | Aggregate expression , It's very powerful , Very complicated | link |
| jsonSchema | Document verification rules , The data of the document itself has no restrictions , Use this to limit | link |
| mod | modulus , Use remainder as query condition | link |
| regex | Regular Expression Matching | link |
| text Text search , But you need to build an index , impractical | link |
边栏推荐
猜你喜欢
随机推荐
c语言字符指针、字符串初始化问题
The latest cloud development wechat balance charger special effect applet source code
go日志包 log的使用
【数字IC/FPGA】检测最后一个匹配序列的位置
c语言之字符串数组
PAT乙级1013
第一性原理(最优解理论)
[learn FPGA programming from scratch -48]: Vision - development and application of intelligent sensors
The choice and trade-off between vector recall and literal recall
How to use the apipost script - global variables
VMware virtual machine bridging connectivity
零基础自学SQL课程 | IF函数
超纲练习题不超纲
Practice torch FX: pytorch based model optimization quantization artifact
vivado VIO IP的用法
ICML 2022: UFRGS |作为最优策略转移基础的乐观线性支持和后继特征
Teach you how to transplant tinyriscv to FPGA
MSP430F5529 单片机 读取 GY-906 红外温度传感器
零基础自学SQL课程 | SQL基本函数大全
[sword finger offer] 47 Maximum value of gifts









