-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_fk.go
56 lines (50 loc) · 1.49 KB
/
mongo_fk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ut
import "go.mongodb.org/mongo-driver/bson"
func MUserFk(local, remark string) bson.D {
return MBuilderFk("user", local, "_id", remark)
}
func MUserInfoFk(localField ...string) []bson.D {
var local = "user_id"
var remark = "userInfo"
switch len(localField) {
case 1:
local = localField[0]
break
case 2:
local = localField[0]
remark = localField[1]
break
}
return MBuildFkUnwind("user", local, "_id", remark)
}
// MBuilderFk 构建通用外键
func MBuilderFk(collection, localField, remoteField, remark string) bson.D {
look := bson.D{{"$lookup", bson.D{
{"from", collection},
{"localField", localField},
{"foreignField", remoteField},
{"as", remark},
}}}
return look
}
// MBuildFkUnwind 构建通用解构外键
func MBuildFkUnwind(collection, localField, remoteField, remark string) []bson.D {
look := MBuilderFk(collection, localField, remoteField, remark)
unwind := bson.D{{
Key: "$unwind",
Value: "$" + remark}}
return []bson.D{look, unwind}
}
// MBuildFkUnwindOfEmptyReturn preserveNullAndEmptyArrays 默认为false 不存在时则不返回
// 文档 https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/
func MBuildFkUnwindOfEmptyReturn(collection, localField, remoteField, remark string) []bson.D {
look := MBuilderFk(collection, localField, remoteField, remark)
unwind := bson.D{{
Key: "$unwind",
Value: bson.M{
"path": "$" + remark,
"preserveNullAndEmptyArrays": true,
},
}}
return []bson.D{look, unwind}
}