@@ -1071,3 +1071,85 @@ func ExampleIndexView_List() {
1071
1071
}
1072
1072
fmt .Println (results )
1073
1073
}
1074
+
1075
+ func ExampleCollection_Find_primitiveRegex () {
1076
+ ctx := context .TODO ()
1077
+ clientOptions := options .Client ().ApplyURI ("mongodb://localhost:27017" )
1078
+
1079
+ // Connect to a mongodb server.
1080
+ client , err := mongo .Connect (ctx , clientOptions )
1081
+ if err != nil {
1082
+ panic (err )
1083
+ }
1084
+
1085
+ defer client .Disconnect (ctx )
1086
+
1087
+ type Pet struct {
1088
+ Type string `bson:"type"`
1089
+ Name string `bson:"name"`
1090
+ }
1091
+
1092
+ // Create a slice of documents to insert. We will lookup a subset of
1093
+ // these documents using regex.
1094
+ toInsert := []interface {}{
1095
+ Pet {Type : "cat" , Name : "Mo" },
1096
+ Pet {Type : "dog" , Name : "Loki" },
1097
+ }
1098
+
1099
+ coll := client .Database ("test" ).Collection ("test" )
1100
+
1101
+ if _ , err := coll .InsertMany (ctx , toInsert ); err != nil {
1102
+ panic (err )
1103
+ }
1104
+
1105
+ // Create a filter to find a document with key "name" and any value that
1106
+ // starts with letter "m". Use the "i" option to indicate
1107
+ // case-insensitivity.
1108
+ filter := bson.D {{"name" , primitive.Regex {Pattern : "^m" , Options : "i" }}}
1109
+
1110
+ _ , err = coll .Find (ctx , filter )
1111
+ if err != nil {
1112
+ panic (err )
1113
+ }
1114
+ }
1115
+
1116
+ func ExampleCollection_Find_regex () {
1117
+ ctx := context .TODO ()
1118
+ clientOptions := options .Client ().ApplyURI ("mongodb://localhost:27017" )
1119
+
1120
+ // Connect to a mongodb server.
1121
+ client , err := mongo .Connect (ctx , clientOptions )
1122
+ if err != nil {
1123
+ panic (err )
1124
+ }
1125
+
1126
+ defer client .Disconnect (ctx )
1127
+
1128
+ type Pet struct {
1129
+ Type string `bson:"type"`
1130
+ Name string `bson:"name"`
1131
+ }
1132
+
1133
+ // Create a slice of documents to insert. We will lookup a subset of
1134
+ // these documents using regex.
1135
+ toInsert := []interface {}{
1136
+ Pet {Type : "cat" , Name : "Mo" },
1137
+ Pet {Type : "dog" , Name : "Loki" },
1138
+ }
1139
+
1140
+ coll := client .Database ("test" ).Collection ("test" )
1141
+
1142
+ if _ , err := coll .InsertMany (ctx , toInsert ); err != nil {
1143
+ panic (err )
1144
+ }
1145
+
1146
+ // Create a filter to find a document with key "name" and any value that
1147
+ // starts with letter "m". Use the "i" option to indicate
1148
+ // case-insensitivity.
1149
+ filter := bson.D {{"name" , bson.D {{"$regex" , "^m" }, {"$options" , "i" }}}}
1150
+
1151
+ _ , err = coll .Find (ctx , filter )
1152
+ if err != nil {
1153
+ panic (err )
1154
+ }
1155
+ }
0 commit comments