1
2
3 package example_commands_test
4
5 import (
6 "context"
7 "fmt"
8
9 "github.com/redis/go-redis/v9"
10 )
11
12
13 func ExampleClient_setget() {
14 ctx := context.Background()
15
16 rdb := redis.NewClient(&redis.Options{
17 Addr: "localhost:6379",
18 Password: "",
19 DB: 0,
20 })
21
22
23
24 rdb.FlushDB(ctx)
25 rdb.Del(ctx, "bike")
26
27
28
29 res1, err := rdb.JSONSet(ctx, "bike", "$",
30 "\"Hyperion\"",
31 ).Result()
32
33 if err != nil {
34 panic(err)
35 }
36
37 fmt.Println(res1)
38
39 res2, err := rdb.JSONGet(ctx, "bike", "$").Result()
40
41 if err != nil {
42 panic(err)
43 }
44
45 fmt.Println(res2)
46
47 res3, err := rdb.JSONType(ctx, "bike", "$").Result()
48
49 if err != nil {
50 panic(err)
51 }
52
53 fmt.Println(res3)
54
55
56
57
58
59
60 }
61
62 func ExampleClient_str() {
63 ctx := context.Background()
64
65 rdb := redis.NewClient(&redis.Options{
66 Addr: "localhost:6379",
67 Password: "",
68 DB: 0,
69 })
70
71
72
73 rdb.FlushDB(ctx)
74 rdb.Del(ctx, "bike")
75
76
77 _, err := rdb.JSONSet(ctx, "bike", "$",
78 "\"Hyperion\"",
79 ).Result()
80
81 if err != nil {
82 panic(err)
83 }
84
85
86 res4, err := rdb.JSONStrLen(ctx, "bike", "$").Result()
87
88 if err != nil {
89 panic(err)
90 }
91
92 fmt.Println(*res4[0])
93
94 res5, err := rdb.JSONStrAppend(ctx, "bike", "$", "\" (Enduro bikes)\"").Result()
95
96 if err != nil {
97 panic(err)
98 }
99
100 fmt.Println(*res5[0])
101
102 res6, err := rdb.JSONGet(ctx, "bike", "$").Result()
103
104 if err != nil {
105 panic(err)
106 }
107
108 fmt.Println(res6)
109
110
111
112
113
114
115 }
116
117 func ExampleClient_num() {
118 ctx := context.Background()
119
120 rdb := redis.NewClient(&redis.Options{
121 Addr: "localhost:6379",
122 Password: "",
123 DB: 0,
124 })
125
126
127
128 rdb.FlushDB(ctx)
129 rdb.Del(ctx, "crashes")
130
131
132
133 res7, err := rdb.JSONSet(ctx, "crashes", "$", 0).Result()
134
135 if err != nil {
136 panic(err)
137 }
138
139 fmt.Println(res7)
140
141 res8, err := rdb.JSONNumIncrBy(ctx, "crashes", "$", 1).Result()
142
143 if err != nil {
144 panic(err)
145 }
146
147 fmt.Println(res8)
148
149 res9, err := rdb.JSONNumIncrBy(ctx, "crashes", "$", 1.5).Result()
150
151 if err != nil {
152 panic(err)
153 }
154
155 fmt.Println(res9)
156
157 res10, err := rdb.JSONNumIncrBy(ctx, "crashes", "$", -0.75).Result()
158
159 if err != nil {
160 panic(err)
161 }
162
163 fmt.Println(res10)
164
165
166
167
168
169
170
171 }
172
173 func ExampleClient_arr() {
174 ctx := context.Background()
175
176 rdb := redis.NewClient(&redis.Options{
177 Addr: "localhost:6379",
178 Password: "",
179 DB: 0,
180 })
181
182
183
184 rdb.FlushDB(ctx)
185 rdb.Del(ctx, "newbike")
186
187
188
189 res11, err := rdb.JSONSet(ctx, "newbike", "$",
190 []interface{}{
191 "Deimos",
192 map[string]interface{}{"crashes": 0},
193 nil,
194 },
195 ).Result()
196
197 if err != nil {
198 panic(err)
199 }
200
201 fmt.Println(res11)
202
203 res12, err := rdb.JSONGet(ctx, "newbike", "$").Result()
204
205 if err != nil {
206 panic(err)
207 }
208
209 fmt.Println(res12)
210
211 res13, err := rdb.JSONGet(ctx, "newbike", "$[1].crashes").Result()
212
213 if err != nil {
214 panic(err)
215 }
216
217 fmt.Println(res13)
218
219 res14, err := rdb.JSONDel(ctx, "newbike", "$.[-1]").Result()
220
221 if err != nil {
222 panic(err)
223 }
224
225 fmt.Println(res14)
226
227 res15, err := rdb.JSONGet(ctx, "newbike", "$").Result()
228
229 if err != nil {
230 panic(err)
231 }
232
233 fmt.Println(res15)
234
235
236
237
238
239
240
241
242 }
243
244 func ExampleClient_arr2() {
245 ctx := context.Background()
246
247 rdb := redis.NewClient(&redis.Options{
248 Addr: "localhost:6379",
249 Password: "",
250 DB: 0,
251 })
252
253
254 rdb.Del(ctx, "riders")
255
256
257
258 res16, err := rdb.JSONSet(ctx, "riders", "$", []interface{}{}).Result()
259
260 if err != nil {
261 panic(err)
262 }
263
264 fmt.Println(res16)
265
266 res17, err := rdb.JSONArrAppend(ctx, "riders", "$", "\"Norem\"").Result()
267
268 if err != nil {
269 panic(err)
270 }
271
272 fmt.Println(res17)
273
274 res18, err := rdb.JSONGet(ctx, "riders", "$").Result()
275
276 if err != nil {
277 panic(err)
278 }
279
280 fmt.Println(res18)
281
282 res19, err := rdb.JSONArrInsert(ctx, "riders", "$", 1,
283 "\"Prickett\"", "\"Royce\"", "\"Castilla\"",
284 ).Result()
285
286 if err != nil {
287 panic(err)
288 }
289
290 fmt.Println(res19)
291
292 res20, err := rdb.JSONGet(ctx, "riders", "$").Result()
293
294 if err != nil {
295 panic(err)
296 }
297
298 fmt.Println(res20)
299
300 rangeStop := 1
301
302 res21, err := rdb.JSONArrTrimWithArgs(ctx, "riders", "$",
303 &redis.JSONArrTrimArgs{Start: 1, Stop: &rangeStop},
304 ).Result()
305
306 if err != nil {
307 panic(err)
308 }
309
310 fmt.Println(res21)
311
312 res22, err := rdb.JSONGet(ctx, "riders", "$").Result()
313
314 if err != nil {
315 panic(err)
316 }
317
318 fmt.Println(res22)
319
320 res23, err := rdb.JSONArrPop(ctx, "riders", "$", -1).Result()
321
322 if err != nil {
323 panic(err)
324 }
325
326 fmt.Println(res23)
327
328 res24, err := rdb.JSONArrPop(ctx, "riders", "$", -1).Result()
329
330 if err != nil {
331 panic(err)
332 }
333
334 fmt.Println(res24)
335
336
337
338
339
340
341
342
343
344
345
346
347 }
348
349 func ExampleClient_obj() {
350 ctx := context.Background()
351
352 rdb := redis.NewClient(&redis.Options{
353 Addr: "localhost:6379",
354 Password: "",
355 DB: 0,
356 })
357
358
359 rdb.Del(ctx, "bike:1")
360
361
362
363 res25, err := rdb.JSONSet(ctx, "bike:1", "$",
364 map[string]interface{}{
365 "model": "Deimos",
366 "brand": "Ergonom",
367 "price": 4972,
368 },
369 ).Result()
370
371 if err != nil {
372 panic(err)
373 }
374
375 fmt.Println(res25)
376
377 res26, err := rdb.JSONObjLen(ctx, "bike:1", "$").Result()
378
379 if err != nil {
380 panic(err)
381 }
382
383 fmt.Println(*res26[0])
384
385 res27, err := rdb.JSONObjKeys(ctx, "bike:1", "$").Result()
386
387 if err != nil {
388 panic(err)
389 }
390
391 fmt.Println(res27)
392
393
394
395
396
397
398 }
399
400 var inventory_json = map[string]interface{}{
401 "inventory": map[string]interface{}{
402 "mountain_bikes": []interface{}{
403 map[string]interface{}{
404 "id": "bike:1",
405 "model": "Phoebe",
406 "description": "This is a mid-travel trail slayer that is a fantastic " +
407 "daily driver or one bike quiver. The Shimano Claris 8-speed groupset " +
408 "gives plenty of gear range to tackle hills and there\u2019s room for " +
409 "mudguards and a rack too. This is the bike for the rider who wants " +
410 "trail manners with low fuss ownership.",
411 "price": 1920,
412 "specs": map[string]interface{}{"material": "carbon", "weight": 13.1},
413 "colors": []interface{}{"black", "silver"},
414 },
415 map[string]interface{}{
416 "id": "bike:2",
417 "model": "Quaoar",
418 "description": "Redesigned for the 2020 model year, this bike " +
419 "impressed our testers and is the best all-around trail bike we've " +
420 "ever tested. The Shimano gear system effectively does away with an " +
421 "external cassette, so is super low maintenance in terms of wear " +
422 "and tear. All in all it's an impressive package for the price, " +
423 "making it very competitive.",
424 "price": 2072,
425 "specs": map[string]interface{}{"material": "aluminium", "weight": 7.9},
426 "colors": []interface{}{"black", "white"},
427 },
428 map[string]interface{}{
429 "id": "bike:3",
430 "model": "Weywot",
431 "description": "This bike gives kids aged six years and older " +
432 "a durable and uberlight mountain bike for their first experience " +
433 "on tracks and easy cruising through forests and fields. A set of " +
434 "powerful Shimano hydraulic disc brakes provide ample stopping " +
435 "ability. If you're after a budget option, this is one of the best " +
436 "bikes you could get.",
437 "price": 3264,
438 "specs": map[string]interface{}{"material": "alloy", "weight": 13.8},
439 },
440 },
441 "commuter_bikes": []interface{}{
442 map[string]interface{}{
443 "id": "bike:4",
444 "model": "Salacia",
445 "description": "This bike is a great option for anyone who just " +
446 "wants a bike to get about on With a slick-shifting Claris gears " +
447 "from Shimano\u2019s, this is a bike which doesn\u2019t break the " +
448 "bank and delivers craved performance. It\u2019s for the rider " +
449 "who wants both efficiency and capability.",
450 "price": 1475,
451 "specs": map[string]interface{}{"material": "aluminium", "weight": 16.6},
452 "colors": []interface{}{"black", "silver"},
453 },
454 map[string]interface{}{
455 "id": "bike:5",
456 "model": "Mimas",
457 "description": "A real joy to ride, this bike got very high " +
458 "scores in last years Bike of the year report. The carefully " +
459 "crafted 50-34 tooth chainset and 11-32 tooth cassette give an " +
460 "easy-on-the-legs bottom gear for climbing, and the high-quality " +
461 "Vittoria Zaffiro tires give balance and grip.It includes " +
462 "a low-step frame , our memory foam seat, bump-resistant shocks and " +
463 "conveniently placed thumb throttle. Put it all together and you " +
464 "get a bike that helps redefine what can be done for this price.",
465 "price": 3941,
466 "specs": map[string]interface{}{"material": "alloy", "weight": 11.6},
467 },
468 },
469 },
470 }
471
472 func ExampleClient_setbikes() {
473 ctx := context.Background()
474
475 rdb := redis.NewClient(&redis.Options{
476 Addr: "localhost:6379",
477 Password: "",
478 DB: 0,
479 })
480
481
482 rdb.Del(ctx, "bikes:inventory")
483
484
485
486 var inventory_json = map[string]interface{}{
487 "inventory": map[string]interface{}{
488 "mountain_bikes": []interface{}{
489 map[string]interface{}{
490 "id": "bike:1",
491 "model": "Phoebe",
492 "description": "This is a mid-travel trail slayer that is a fantastic " +
493 "daily driver or one bike quiver. The Shimano Claris 8-speed groupset " +
494 "gives plenty of gear range to tackle hills and there\u2019s room for " +
495 "mudguards and a rack too. This is the bike for the rider who wants " +
496 "trail manners with low fuss ownership.",
497 "price": 1920,
498 "specs": map[string]interface{}{"material": "carbon", "weight": 13.1},
499 "colors": []interface{}{"black", "silver"},
500 },
501 map[string]interface{}{
502 "id": "bike:2",
503 "model": "Quaoar",
504 "description": "Redesigned for the 2020 model year, this bike " +
505 "impressed our testers and is the best all-around trail bike we've " +
506 "ever tested. The Shimano gear system effectively does away with an " +
507 "external cassette, so is super low maintenance in terms of wear " +
508 "and tear. All in all it's an impressive package for the price, " +
509 "making it very competitive.",
510 "price": 2072,
511 "specs": map[string]interface{}{"material": "aluminium", "weight": 7.9},
512 "colors": []interface{}{"black", "white"},
513 },
514 map[string]interface{}{
515 "id": "bike:3",
516 "model": "Weywot",
517 "description": "This bike gives kids aged six years and older " +
518 "a durable and uberlight mountain bike for their first experience " +
519 "on tracks and easy cruising through forests and fields. A set of " +
520 "powerful Shimano hydraulic disc brakes provide ample stopping " +
521 "ability. If you're after a budget option, this is one of the best " +
522 "bikes you could get.",
523 "price": 3264,
524 "specs": map[string]interface{}{"material": "alloy", "weight": 13.8},
525 },
526 },
527 "commuter_bikes": []interface{}{
528 map[string]interface{}{
529 "id": "bike:4",
530 "model": "Salacia",
531 "description": "This bike is a great option for anyone who just " +
532 "wants a bike to get about on With a slick-shifting Claris gears " +
533 "from Shimano\u2019s, this is a bike which doesn\u2019t break the " +
534 "bank and delivers craved performance. It\u2019s for the rider " +
535 "who wants both efficiency and capability.",
536 "price": 1475,
537 "specs": map[string]interface{}{"material": "aluminium", "weight": 16.6},
538 "colors": []interface{}{"black", "silver"},
539 },
540 map[string]interface{}{
541 "id": "bike:5",
542 "model": "Mimas",
543 "description": "A real joy to ride, this bike got very high " +
544 "scores in last years Bike of the year report. The carefully " +
545 "crafted 50-34 tooth chainset and 11-32 tooth cassette give an " +
546 "easy-on-the-legs bottom gear for climbing, and the high-quality " +
547 "Vittoria Zaffiro tires give balance and grip.It includes " +
548 "a low-step frame , our memory foam seat, bump-resistant shocks and " +
549 "conveniently placed thumb throttle. Put it all together and you " +
550 "get a bike that helps redefine what can be done for this price.",
551 "price": 3941,
552 "specs": map[string]interface{}{"material": "alloy", "weight": 11.6},
553 },
554 },
555 },
556 }
557
558 res1, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
559
560 if err != nil {
561 panic(err)
562 }
563
564 fmt.Println(res1)
565
566
567
568
569 }
570
571 func ExampleClient_getbikes() {
572 ctx := context.Background()
573
574 rdb := redis.NewClient(&redis.Options{
575 Addr: "localhost:6379",
576 Password: "",
577 DB: 0,
578 })
579
580
581 rdb.Del(ctx, "bikes:inventory")
582
583
584 _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
585
586 if err != nil {
587 panic(err)
588 }
589
590
591 res2, err := rdb.JSONGetWithArgs(ctx, "bikes:inventory",
592 &redis.JSONGetArgs{Indent: " ", Newline: "\n", Space: " "},
593 "$.inventory.*",
594 ).Result()
595
596 if err != nil {
597 panic(err)
598 }
599
600 fmt.Println(res2)
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680 }
681
682 func ExampleClient_getmtnbikes() {
683 ctx := context.Background()
684
685 rdb := redis.NewClient(&redis.Options{
686 Addr: "localhost:6379",
687 Password: "",
688 DB: 0,
689 })
690
691
692 rdb.Del(ctx, "bikes:inventory")
693
694
695 _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
696
697 if err != nil {
698 panic(err)
699 }
700
701
702 res3, err := rdb.JSONGet(ctx, "bikes:inventory",
703 "$.inventory.mountain_bikes[*].model",
704 ).Result()
705
706 if err != nil {
707 panic(err)
708 }
709
710 fmt.Println(res3)
711
712
713 res4, err := rdb.JSONGet(ctx,
714 "bikes:inventory", "$.inventory[\"mountain_bikes\"][*].model",
715 ).Result()
716
717 if err != nil {
718 panic(err)
719 }
720
721 fmt.Println(res4)
722
723
724 res5, err := rdb.JSONGet(ctx,
725 "bikes:inventory", "$..mountain_bikes[*].model",
726 ).Result()
727
728 if err != nil {
729 panic(err)
730 }
731
732 fmt.Println(res5)
733
734
735
736
737
738
739
740 }
741
742 func ExampleClient_getmodels() {
743 ctx := context.Background()
744
745 rdb := redis.NewClient(&redis.Options{
746 Addr: "localhost:6379",
747 Password: "",
748 DB: 0,
749 })
750
751
752 rdb.Del(ctx, "bikes:inventory")
753
754
755 _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
756
757 if err != nil {
758 panic(err)
759 }
760
761
762 res6, err := rdb.JSONGet(ctx, "bikes:inventory", "$..model").Result()
763
764 if err != nil {
765 panic(err)
766 }
767
768 fmt.Println(res6)
769
770
771
772
773 }
774
775 func ExampleClient_get2mtnbikes() {
776 ctx := context.Background()
777
778 rdb := redis.NewClient(&redis.Options{
779 Addr: "localhost:6379",
780 Password: "",
781 DB: 0,
782 })
783
784
785 rdb.Del(ctx, "bikes:inventory")
786
787
788 _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
789
790 if err != nil {
791 panic(err)
792 }
793
794
795 res7, err := rdb.JSONGet(ctx, "bikes:inventory", "$..mountain_bikes[0:2].model").Result()
796
797 if err != nil {
798 panic(err)
799 }
800
801 fmt.Println(res7)
802
803
804
805
806 }
807
808 func ExampleClient_filter1() {
809 ctx := context.Background()
810
811 rdb := redis.NewClient(&redis.Options{
812 Addr: "localhost:6379",
813 Password: "",
814 DB: 0,
815 })
816
817
818 rdb.Del(ctx, "bikes:inventory")
819
820
821 _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
822
823 if err != nil {
824 panic(err)
825 }
826
827
828 res8, err := rdb.JSONGetWithArgs(ctx, "bikes:inventory",
829 &redis.JSONGetArgs{Indent: " ", Newline: "\n", Space: " "},
830 "$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]",
831 ).Result()
832
833 if err != nil {
834 panic(err)
835 }
836
837 fmt.Println(res8)
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866 }
867
868 func ExampleClient_filter2() {
869 ctx := context.Background()
870
871 rdb := redis.NewClient(&redis.Options{
872 Addr: "localhost:6379",
873 Password: "",
874 DB: 0,
875 })
876
877
878 rdb.Del(ctx, "bikes:inventory")
879
880
881 _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
882
883 if err != nil {
884 panic(err)
885 }
886
887
888 res9, err := rdb.JSONGet(ctx,
889 "bikes:inventory",
890 "$..[?(@.specs.material == 'alloy')].model",
891 ).Result()
892
893 if err != nil {
894 panic(err)
895 }
896
897 fmt.Println(res9)
898
899
900
901
902 }
903
904 func ExampleClient_filter3() {
905 ctx := context.Background()
906
907 rdb := redis.NewClient(&redis.Options{
908 Addr: "localhost:6379",
909 Password: "",
910 DB: 0,
911 })
912
913
914 rdb.Del(ctx, "bikes:inventory")
915
916
917 _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
918
919 if err != nil {
920 panic(err)
921 }
922
923
924 res10, err := rdb.JSONGet(ctx,
925 "bikes:inventory",
926 "$..[?(@.specs.material =~ '(?i)al')].model",
927 ).Result()
928
929 if err != nil {
930 panic(err)
931 }
932
933 fmt.Println(res10)
934
935
936
937
938 }
939
940 func ExampleClient_filter4() {
941 ctx := context.Background()
942
943 rdb := redis.NewClient(&redis.Options{
944 Addr: "localhost:6379",
945 Password: "",
946 DB: 0,
947 })
948
949
950 rdb.Del(ctx, "bikes:inventory")
951
952
953 _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
954
955 if err != nil {
956 panic(err)
957 }
958
959
960 res11, err := rdb.JSONSet(ctx,
961 "bikes:inventory",
962 "$.inventory.mountain_bikes[0].regex_pat",
963 "\"(?i)al\"",
964 ).Result()
965
966 if err != nil {
967 panic(err)
968 }
969
970 fmt.Println(res11)
971
972 res12, err := rdb.JSONSet(ctx,
973 "bikes:inventory",
974 "$.inventory.mountain_bikes[1].regex_pat",
975 "\"(?i)al\"",
976 ).Result()
977
978 if err != nil {
979 panic(err)
980 }
981
982 fmt.Println(res12)
983
984 res13, err := rdb.JSONSet(ctx,
985 "bikes:inventory",
986 "$.inventory.mountain_bikes[2].regex_pat",
987 "\"(?i)al\"",
988 ).Result()
989
990 if err != nil {
991 panic(err)
992 }
993
994 fmt.Println(res13)
995
996 res14, err := rdb.JSONGet(ctx,
997 "bikes:inventory",
998 "$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model",
999 ).Result()
1000
1001 if err != nil {
1002 panic(err)
1003 }
1004
1005 fmt.Println(res14)
1006
1007
1008
1009
1010
1011
1012
1013 }
1014
1015 func ExampleClient_updatebikes() {
1016 ctx := context.Background()
1017
1018 rdb := redis.NewClient(&redis.Options{
1019 Addr: "localhost:6379",
1020 Password: "",
1021 DB: 0,
1022 })
1023
1024
1025 rdb.Del(ctx, "bikes:inventory")
1026
1027
1028 _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
1029
1030 if err != nil {
1031 panic(err)
1032 }
1033
1034
1035 res15, err := rdb.JSONGet(ctx, "bikes:inventory", "$..price").Result()
1036
1037 if err != nil {
1038 panic(err)
1039 }
1040
1041 fmt.Println(res15)
1042
1043 res16, err := rdb.JSONNumIncrBy(ctx, "bikes:inventory", "$..price", -100).Result()
1044
1045 if err != nil {
1046 panic(err)
1047 }
1048
1049 fmt.Println(res16)
1050
1051 res17, err := rdb.JSONNumIncrBy(ctx, "bikes:inventory", "$..price", 100).Result()
1052
1053 if err != nil {
1054 panic(err)
1055 }
1056
1057 fmt.Println(res17)
1058
1059
1060
1061
1062
1063
1064 }
1065
1066 func ExampleClient_updatefilters1() {
1067 ctx := context.Background()
1068
1069 rdb := redis.NewClient(&redis.Options{
1070 Addr: "localhost:6379",
1071 Password: "",
1072 DB: 0,
1073 })
1074
1075
1076 rdb.Del(ctx, "bikes:inventory")
1077
1078
1079 _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
1080
1081 if err != nil {
1082 panic(err)
1083 }
1084
1085
1086 res18, err := rdb.JSONSet(ctx,
1087 "bikes:inventory",
1088 "$.inventory.*[?(@.price<2000)].price",
1089 1500,
1090 ).Result()
1091
1092 if err != nil {
1093 panic(err)
1094 }
1095
1096 fmt.Println(res18)
1097
1098 res19, err := rdb.JSONGet(ctx, "bikes:inventory", "$..price").Result()
1099
1100 if err != nil {
1101 panic(err)
1102 }
1103
1104 fmt.Println(res19)
1105
1106
1107
1108
1109
1110 }
1111
1112 func ExampleClient_updatefilters2() {
1113 ctx := context.Background()
1114
1115 rdb := redis.NewClient(&redis.Options{
1116 Addr: "localhost:6379",
1117 Password: "",
1118 DB: 0,
1119 })
1120
1121
1122 rdb.Del(ctx, "bikes:inventory")
1123
1124
1125 _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
1126
1127 if err != nil {
1128 panic(err)
1129 }
1130
1131
1132 res20, err := rdb.JSONArrAppend(ctx,
1133 "bikes:inventory",
1134 "$.inventory.*[?(@.price<2000)].colors",
1135 "\"pink\"",
1136 ).Result()
1137
1138 if err != nil {
1139 panic(err)
1140 }
1141
1142 fmt.Println(res20)
1143
1144 res21, err := rdb.JSONGet(ctx, "bikes:inventory", "$..[*].colors").Result()
1145
1146 if err != nil {
1147 panic(err)
1148 }
1149
1150 fmt.Println(res21)
1151
1152
1153
1154
1155
1156
1157 }
1158
View as plain text