...

Source file src/github.com/redis/go-redis/v9/doctests/json_tutorial_test.go

Documentation: github.com/redis/go-redis/v9/doctests

     1  // EXAMPLE: json_tutorial
     2  // HIDE_START
     3  package example_commands_test
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  
     9  	"github.com/redis/go-redis/v9"
    10  )
    11  
    12  // HIDE_END
    13  func ExampleClient_setget() {
    14  	ctx := context.Background()
    15  
    16  	rdb := redis.NewClient(&redis.Options{
    17  		Addr:     "localhost:6379",
    18  		Password: "", // no password docs
    19  		DB:       0,  // use default DB
    20  	})
    21  
    22  	// REMOVE_START
    23  	// make sure we are working with fresh database
    24  	rdb.FlushDB(ctx)
    25  	rdb.Del(ctx, "bike")
    26  	// REMOVE_END
    27  
    28  	// STEP_START set_get
    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) // >>> OK
    38  
    39  	res2, err := rdb.JSONGet(ctx, "bike", "$").Result()
    40  
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  
    45  	fmt.Println(res2) // >>> ["Hyperion"]
    46  
    47  	res3, err := rdb.JSONType(ctx, "bike", "$").Result()
    48  
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	fmt.Println(res3) // >>> [[string]]
    54  	// STEP_END
    55  
    56  	// Output:
    57  	// OK
    58  	// ["Hyperion"]
    59  	// [[string]]
    60  }
    61  
    62  func ExampleClient_str() {
    63  	ctx := context.Background()
    64  
    65  	rdb := redis.NewClient(&redis.Options{
    66  		Addr:     "localhost:6379",
    67  		Password: "", // no password docs
    68  		DB:       0,  // use default DB
    69  	})
    70  
    71  	// REMOVE_START
    72  	// start with fresh database
    73  	rdb.FlushDB(ctx)
    74  	rdb.Del(ctx, "bike")
    75  	// REMOVE_END
    76  
    77  	_, err := rdb.JSONSet(ctx, "bike", "$",
    78  		"\"Hyperion\"",
    79  	).Result()
    80  
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  
    85  	// STEP_START str
    86  	res4, err := rdb.JSONStrLen(ctx, "bike", "$").Result()
    87  
    88  	if err != nil {
    89  		panic(err)
    90  	}
    91  
    92  	fmt.Println(*res4[0]) // >>> 8
    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]) // >>> 23
   101  
   102  	res6, err := rdb.JSONGet(ctx, "bike", "$").Result()
   103  
   104  	if err != nil {
   105  		panic(err)
   106  	}
   107  
   108  	fmt.Println(res6) // >>> ["Hyperion (Enduro bikes)"]
   109  	// STEP_END
   110  
   111  	// Output:
   112  	// 8
   113  	// 23
   114  	// ["Hyperion (Enduro bikes)"]
   115  }
   116  
   117  func ExampleClient_num() {
   118  	ctx := context.Background()
   119  
   120  	rdb := redis.NewClient(&redis.Options{
   121  		Addr:     "localhost:6379",
   122  		Password: "", // no password docs
   123  		DB:       0,  // use default DB
   124  	})
   125  
   126  	// REMOVE_START
   127  	// start with fresh database
   128  	rdb.FlushDB(ctx)
   129  	rdb.Del(ctx, "crashes")
   130  	// REMOVE_END
   131  
   132  	// STEP_START num
   133  	res7, err := rdb.JSONSet(ctx, "crashes", "$", 0).Result()
   134  
   135  	if err != nil {
   136  		panic(err)
   137  	}
   138  
   139  	fmt.Println(res7) // >>> OK
   140  
   141  	res8, err := rdb.JSONNumIncrBy(ctx, "crashes", "$", 1).Result()
   142  
   143  	if err != nil {
   144  		panic(err)
   145  	}
   146  
   147  	fmt.Println(res8) // >>> [1]
   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) // >>> [2.5]
   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) // >>> [1.75]
   164  	// STEP_END
   165  
   166  	// Output:
   167  	// OK
   168  	// [1]
   169  	// [2.5]
   170  	// [1.75]
   171  }
   172  
   173  func ExampleClient_arr() {
   174  	ctx := context.Background()
   175  
   176  	rdb := redis.NewClient(&redis.Options{
   177  		Addr:     "localhost:6379",
   178  		Password: "", // no password docs
   179  		DB:       0,  // use default DB
   180  	})
   181  
   182  	// REMOVE_START
   183  	// start with fresh database
   184  	rdb.FlushDB(ctx)
   185  	rdb.Del(ctx, "newbike")
   186  	// REMOVE_END
   187  
   188  	// STEP_START arr
   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) // >>> OK
   202  
   203  	res12, err := rdb.JSONGet(ctx, "newbike", "$").Result()
   204  
   205  	if err != nil {
   206  		panic(err)
   207  	}
   208  
   209  	fmt.Println(res12) // >>> [["Deimos",{"crashes":0},null]]
   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) // >>> [0]
   218  
   219  	res14, err := rdb.JSONDel(ctx, "newbike", "$.[-1]").Result()
   220  
   221  	if err != nil {
   222  		panic(err)
   223  	}
   224  
   225  	fmt.Println(res14) // >>> 1
   226  
   227  	res15, err := rdb.JSONGet(ctx, "newbike", "$").Result()
   228  
   229  	if err != nil {
   230  		panic(err)
   231  	}
   232  
   233  	fmt.Println(res15) // >>> [["Deimos",{"crashes":0}]]
   234  	// STEP_END
   235  
   236  	// Output:
   237  	// OK
   238  	// [["Deimos",{"crashes":0},null]]
   239  	// [0]
   240  	// 1
   241  	// [["Deimos",{"crashes":0}]]
   242  }
   243  
   244  func ExampleClient_arr2() {
   245  	ctx := context.Background()
   246  
   247  	rdb := redis.NewClient(&redis.Options{
   248  		Addr:     "localhost:6379",
   249  		Password: "", // no password docs
   250  		DB:       0,  // use default DB
   251  	})
   252  
   253  	// REMOVE_START
   254  	rdb.Del(ctx, "riders")
   255  	// REMOVE_END
   256  
   257  	// STEP_START arr2
   258  	res16, err := rdb.JSONSet(ctx, "riders", "$", []interface{}{}).Result()
   259  
   260  	if err != nil {
   261  		panic(err)
   262  	}
   263  
   264  	fmt.Println(res16) // >>> OK
   265  
   266  	res17, err := rdb.JSONArrAppend(ctx, "riders", "$", "\"Norem\"").Result()
   267  
   268  	if err != nil {
   269  		panic(err)
   270  	}
   271  
   272  	fmt.Println(res17) // >>> [1]
   273  
   274  	res18, err := rdb.JSONGet(ctx, "riders", "$").Result()
   275  
   276  	if err != nil {
   277  		panic(err)
   278  	}
   279  
   280  	fmt.Println(res18) // >>> [["Norem"]]
   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) // [3]
   291  
   292  	res20, err := rdb.JSONGet(ctx, "riders", "$").Result()
   293  
   294  	if err != nil {
   295  		panic(err)
   296  	}
   297  
   298  	fmt.Println(res20) // >>> [["Norem", "Prickett", "Royce", "Castilla"]]
   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) // >>> [1]
   311  
   312  	res22, err := rdb.JSONGet(ctx, "riders", "$").Result()
   313  
   314  	if err != nil {
   315  		panic(err)
   316  	}
   317  
   318  	fmt.Println(res22) // >>> [["Prickett"]]
   319  
   320  	res23, err := rdb.JSONArrPop(ctx, "riders", "$", -1).Result()
   321  
   322  	if err != nil {
   323  		panic(err)
   324  	}
   325  
   326  	fmt.Println(res23) // >>> [["Prickett"]]
   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  	// STEP_END
   336  
   337  	// Output:
   338  	// OK
   339  	// [1]
   340  	// [["Norem"]]
   341  	// [4]
   342  	// [["Norem","Prickett","Royce","Castilla"]]
   343  	// [1]
   344  	// [["Prickett"]]
   345  	// ["Prickett"]
   346  	// []
   347  }
   348  
   349  func ExampleClient_obj() {
   350  	ctx := context.Background()
   351  
   352  	rdb := redis.NewClient(&redis.Options{
   353  		Addr:     "localhost:6379",
   354  		Password: "", // no password docs
   355  		DB:       0,  // use default DB
   356  	})
   357  
   358  	// REMOVE_START
   359  	rdb.Del(ctx, "bike:1")
   360  	// REMOVE_END
   361  
   362  	// STEP_START obj
   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) // >>> OK
   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]) // >>> 3
   384  
   385  	res27, err := rdb.JSONObjKeys(ctx, "bike:1", "$").Result()
   386  
   387  	if err != nil {
   388  		panic(err)
   389  	}
   390  
   391  	fmt.Println(res27) // >>> [brand model price]
   392  	// STEP_END
   393  
   394  	// Output:
   395  	// OK
   396  	// 3
   397  	// [[brand model price]]
   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: "", // no password docs
   478  		DB:       0,  // use default DB
   479  	})
   480  
   481  	// REMOVE_START
   482  	rdb.Del(ctx, "bikes:inventory")
   483  	// REMOVE_END
   484  
   485  	// STEP_START set_bikes
   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) // >>> OK
   565  	// STEP_END
   566  
   567  	// Output:
   568  	// OK
   569  }
   570  
   571  func ExampleClient_getbikes() {
   572  	ctx := context.Background()
   573  
   574  	rdb := redis.NewClient(&redis.Options{
   575  		Addr:     "localhost:6379",
   576  		Password: "", // no password docs
   577  		DB:       0,  // use default DB
   578  	})
   579  
   580  	// REMOVE_START
   581  	rdb.Del(ctx, "bikes:inventory")
   582  	// REMOVE_END
   583  
   584  	_, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
   585  
   586  	if err != nil {
   587  		panic(err)
   588  	}
   589  
   590  	// STEP_START get_bikes
   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  	//       "colors": [
   606  	//         "black",
   607  	//         "silver"
   608  	// ...
   609  	// STEP_END
   610  
   611  	// Output:
   612  	// [
   613  	//   [
   614  	//     {
   615  	//       "colors": [
   616  	//         "black",
   617  	//         "silver"
   618  	//       ],
   619  	//       "description": "This bike is a great option for anyone who just wants a bike to get about on With a slick-shifting Claris gears from Shimano’s, this is a bike which doesn’t break the bank and delivers craved performance.  It’s for the rider who wants both efficiency and capability.",
   620  	//       "id": "bike:4",
   621  	//       "model": "Salacia",
   622  	//       "price": 1475,
   623  	//       "specs": {
   624  	//         "material": "aluminium",
   625  	//         "weight": 16.6
   626  	//       }
   627  	//     },
   628  	//     {
   629  	//       "description": "A real joy to ride, this bike got very high scores in last years Bike of the year report. The carefully crafted 50-34 tooth chainset and 11-32 tooth cassette give an easy-on-the-legs bottom gear for climbing, and the high-quality Vittoria Zaffiro tires give balance and grip.It includes a low-step frame , our memory foam seat, bump-resistant shocks and conveniently placed thumb throttle. Put it all together and you get a bike that helps redefine what can be done for this price.",
   630  	//       "id": "bike:5",
   631  	//       "model": "Mimas",
   632  	//       "price": 3941,
   633  	//       "specs": {
   634  	//         "material": "alloy",
   635  	//         "weight": 11.6
   636  	//       }
   637  	//     }
   638  	//   ],
   639  	//   [
   640  	//     {
   641  	//       "colors": [
   642  	//         "black",
   643  	//         "silver"
   644  	//       ],
   645  	//       "description": "This is a mid-travel trail slayer that is a fantastic daily driver or one bike quiver. The Shimano Claris 8-speed groupset gives plenty of gear range to tackle hills and there’s room for mudguards and a rack too.  This is the bike for the rider who wants trail manners with low fuss ownership.",
   646  	//       "id": "bike:1",
   647  	//       "model": "Phoebe",
   648  	//       "price": 1920,
   649  	//       "specs": {
   650  	//         "material": "carbon",
   651  	//         "weight": 13.1
   652  	//       }
   653  	//     },
   654  	//     {
   655  	//       "colors": [
   656  	//         "black",
   657  	//         "white"
   658  	//       ],
   659  	//       "description": "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and tear. All in all it's an impressive package for the price, making it very competitive.",
   660  	//       "id": "bike:2",
   661  	//       "model": "Quaoar",
   662  	//       "price": 2072,
   663  	//       "specs": {
   664  	//         "material": "aluminium",
   665  	//         "weight": 7.9
   666  	//       }
   667  	//     },
   668  	//     {
   669  	//       "description": "This bike gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. A set of powerful Shimano hydraulic disc brakes provide ample stopping ability. If you're after a budget option, this is one of the best bikes you could get.",
   670  	//       "id": "bike:3",
   671  	//       "model": "Weywot",
   672  	//       "price": 3264,
   673  	//       "specs": {
   674  	//         "material": "alloy",
   675  	//         "weight": 13.8
   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: "", // no password docs
   688  		DB:       0,  // use default DB
   689  	})
   690  
   691  	// REMOVE_START
   692  	rdb.Del(ctx, "bikes:inventory")
   693  	// REMOVE_END
   694  
   695  	_, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
   696  
   697  	if err != nil {
   698  		panic(err)
   699  	}
   700  
   701  	// STEP_START get_mtnbikes
   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  	// >>> ["Phoebe","Quaoar","Weywot"]
   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  	// >>> ["Phoebe","Quaoar","Weywot"]
   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  	// >>> ["Phoebe","Quaoar","Weywot"]
   734  	// STEP_END
   735  
   736  	// Output:
   737  	// ["Phoebe","Quaoar","Weywot"]
   738  	// ["Phoebe","Quaoar","Weywot"]
   739  	// ["Phoebe","Quaoar","Weywot"]
   740  }
   741  
   742  func ExampleClient_getmodels() {
   743  	ctx := context.Background()
   744  
   745  	rdb := redis.NewClient(&redis.Options{
   746  		Addr:     "localhost:6379",
   747  		Password: "", // no password docs
   748  		DB:       0,  // use default DB
   749  	})
   750  
   751  	// REMOVE_START
   752  	rdb.Del(ctx, "bikes:inventory")
   753  	// REMOVE_END
   754  
   755  	_, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
   756  
   757  	if err != nil {
   758  		panic(err)
   759  	}
   760  
   761  	// STEP_START get_models
   762  	res6, err := rdb.JSONGet(ctx, "bikes:inventory", "$..model").Result()
   763  
   764  	if err != nil {
   765  		panic(err)
   766  	}
   767  
   768  	fmt.Println(res6) // >>> ["Salacia","Mimas","Phoebe","Quaoar","Weywot"]
   769  	// STEP_END
   770  
   771  	// Output:
   772  	// ["Salacia","Mimas","Phoebe","Quaoar","Weywot"]
   773  }
   774  
   775  func ExampleClient_get2mtnbikes() {
   776  	ctx := context.Background()
   777  
   778  	rdb := redis.NewClient(&redis.Options{
   779  		Addr:     "localhost:6379",
   780  		Password: "", // no password docs
   781  		DB:       0,  // use default DB
   782  	})
   783  
   784  	// REMOVE_START
   785  	rdb.Del(ctx, "bikes:inventory")
   786  	// REMOVE_END
   787  
   788  	_, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
   789  
   790  	if err != nil {
   791  		panic(err)
   792  	}
   793  
   794  	// STEP_START get2mtnbikes
   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) // >>> ["Phoebe","Quaoar"]
   802  	// STEP_END
   803  
   804  	// Output:
   805  	// ["Phoebe","Quaoar"]
   806  }
   807  
   808  func ExampleClient_filter1() {
   809  	ctx := context.Background()
   810  
   811  	rdb := redis.NewClient(&redis.Options{
   812  		Addr:     "localhost:6379",
   813  		Password: "", // no password docs
   814  		DB:       0,  // use default DB
   815  	})
   816  
   817  	// REMOVE_START
   818  	rdb.Del(ctx, "bikes:inventory")
   819  	// REMOVE_END
   820  
   821  	_, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
   822  
   823  	if err != nil {
   824  		panic(err)
   825  	}
   826  
   827  	// STEP_START filter1
   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  	//     "colors": [
   842  	//       "black",
   843  	//       "white"
   844  	//     ],
   845  	//     "description": "Redesigned for the 2020 model year
   846  	// ...
   847  	// STEP_END
   848  
   849  	// Output:
   850  	// [
   851  	//   {
   852  	//     "colors": [
   853  	//       "black",
   854  	//       "white"
   855  	//     ],
   856  	//     "description": "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and tear. All in all it's an impressive package for the price, making it very competitive.",
   857  	//     "id": "bike:2",
   858  	//     "model": "Quaoar",
   859  	//     "price": 2072,
   860  	//     "specs": {
   861  	//       "material": "aluminium",
   862  	//       "weight": 7.9
   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: "", // no password docs
   874  		DB:       0,  // use default DB
   875  	})
   876  
   877  	// REMOVE_START
   878  	rdb.Del(ctx, "bikes:inventory")
   879  	// REMOVE_END
   880  
   881  	_, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
   882  
   883  	if err != nil {
   884  		panic(err)
   885  	}
   886  
   887  	// STEP_START filter2
   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) // >>> ["Mimas","Weywot"]
   898  	// STEP_END
   899  
   900  	// Output:
   901  	// ["Mimas","Weywot"]
   902  }
   903  
   904  func ExampleClient_filter3() {
   905  	ctx := context.Background()
   906  
   907  	rdb := redis.NewClient(&redis.Options{
   908  		Addr:     "localhost:6379",
   909  		Password: "", // no password docs
   910  		DB:       0,  // use default DB
   911  	})
   912  
   913  	// REMOVE_START
   914  	rdb.Del(ctx, "bikes:inventory")
   915  	// REMOVE_END
   916  
   917  	_, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
   918  
   919  	if err != nil {
   920  		panic(err)
   921  	}
   922  
   923  	// STEP_START filter3
   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) // >>> ["Salacia","Mimas","Quaoar","Weywot"]
   934  	// STEP_END
   935  
   936  	// Output:
   937  	// ["Salacia","Mimas","Quaoar","Weywot"]
   938  }
   939  
   940  func ExampleClient_filter4() {
   941  	ctx := context.Background()
   942  
   943  	rdb := redis.NewClient(&redis.Options{
   944  		Addr:     "localhost:6379",
   945  		Password: "", // no password docs
   946  		DB:       0,  // use default DB
   947  	})
   948  
   949  	// REMOVE_START
   950  	rdb.Del(ctx, "bikes:inventory")
   951  	// REMOVE_END
   952  
   953  	_, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
   954  
   955  	if err != nil {
   956  		panic(err)
   957  	}
   958  
   959  	// STEP_START filter4
   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) // >>> OK
   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) // >>> OK
   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) // >>> OK
   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) // >>> ["Quaoar","Weywot"]
  1006  	// STEP_END
  1007  
  1008  	// Output:
  1009  	// OK
  1010  	// OK
  1011  	// OK
  1012  	// ["Quaoar","Weywot"]
  1013  }
  1014  
  1015  func ExampleClient_updatebikes() {
  1016  	ctx := context.Background()
  1017  
  1018  	rdb := redis.NewClient(&redis.Options{
  1019  		Addr:     "localhost:6379",
  1020  		Password: "", // no password docs
  1021  		DB:       0,  // use default DB
  1022  	})
  1023  
  1024  	// REMOVE_START
  1025  	rdb.Del(ctx, "bikes:inventory")
  1026  	// REMOVE_END
  1027  
  1028  	_, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
  1029  
  1030  	if err != nil {
  1031  		panic(err)
  1032  	}
  1033  
  1034  	// STEP_START update_bikes
  1035  	res15, err := rdb.JSONGet(ctx, "bikes:inventory", "$..price").Result()
  1036  
  1037  	if err != nil {
  1038  		panic(err)
  1039  	}
  1040  
  1041  	fmt.Println(res15) // >>> [1475,3941,1920,2072,3264]
  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) // >>> [1375,3841,1820,1972,3164]
  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) // >>> [1475,3941,1920,2072,3264]
  1058  	// STEP_END
  1059  
  1060  	// Output:
  1061  	// [1475,3941,1920,2072,3264]
  1062  	// [1375,3841,1820,1972,3164]
  1063  	// [1475,3941,1920,2072,3264]
  1064  }
  1065  
  1066  func ExampleClient_updatefilters1() {
  1067  	ctx := context.Background()
  1068  
  1069  	rdb := redis.NewClient(&redis.Options{
  1070  		Addr:     "localhost:6379",
  1071  		Password: "", // no password docs
  1072  		DB:       0,  // use default DB
  1073  	})
  1074  
  1075  	// REMOVE_START
  1076  	rdb.Del(ctx, "bikes:inventory")
  1077  	// REMOVE_END
  1078  
  1079  	_, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
  1080  
  1081  	if err != nil {
  1082  		panic(err)
  1083  	}
  1084  
  1085  	// STEP_START update_filters1
  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) // >>> OK
  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) // >>> [1500,3941,1500,2072,3264]
  1105  	// STEP_END
  1106  
  1107  	// Output:
  1108  	// OK
  1109  	// [1500,3941,1500,2072,3264]
  1110  }
  1111  
  1112  func ExampleClient_updatefilters2() {
  1113  	ctx := context.Background()
  1114  
  1115  	rdb := redis.NewClient(&redis.Options{
  1116  		Addr:     "localhost:6379",
  1117  		Password: "", // no password docs
  1118  		DB:       0,  // use default DB
  1119  	})
  1120  
  1121  	// REMOVE_START
  1122  	rdb.Del(ctx, "bikes:inventory")
  1123  	// REMOVE_END
  1124  
  1125  	_, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result()
  1126  
  1127  	if err != nil {
  1128  		panic(err)
  1129  	}
  1130  
  1131  	// STEP_START update_filters2
  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) // >>> [3 3]
  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  	// >>> [["black","silver","pink"],["black","silver","pink"],["black","white"]]
  1152  	// STEP_END
  1153  
  1154  	// Output:
  1155  	// [3 3]
  1156  	// [["black","silver","pink"],["black","silver","pink"],["black","white"]]
  1157  }
  1158  

View as plain text