1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

Updates examples. (#2219)

* Updates examples.

* Added command link for hset.
This commit is contained in:
Simon Prickett
2022-08-15 13:39:28 +01:00
committed by GitHub
parent 60ad6aab0b
commit f3462abf33
19 changed files with 972 additions and 1019 deletions

View File

@@ -2,80 +2,75 @@
import { createClient } from 'redis';
async function managingJSON() {
const client = createClient();
const client = createClient();
await client.connect();
await client.del('noderedis:jsondata');
await client.connect();
await client.del('noderedis:jsondata');
// Store a JSON object...
await client.json.set('noderedis:jsondata', '$', {
name: 'Roberta McDonald',
pets: [
{
name: 'Fluffy',
species: 'dog',
age: 5,
isMammal: true
},
{
name: 'Rex',
species: 'dog',
age: 3,
isMammal: true
},
{
name: 'Goldie',
species: 'fish',
age: 2,
isMammal: false
}
],
address: {
number: 99,
street: 'Main Street',
city: 'Springfield',
state: 'OH',
country: 'USA'
// Store a JSON object...
await client.json.set('noderedis:jsondata', '$', {
name: 'Roberta McDonald',
pets: [
{
name: 'Fluffy',
species: 'dog',
age: 5,
isMammal: true
},
{
name: 'Rex',
species: 'dog',
age: 3,
isMammal: true
},
{
name: 'Goldie',
species: 'fish',
age: 2,
isMammal: false
}
});
],
address: {
number: 99,
street: 'Main Street',
city: 'Springfield',
state: 'OH',
country: 'USA'
}
});
// Retrieve the name and age of the second pet in the pets array.
let results = await client.json.get('noderedis:jsondata', {
path: [
'.pets[1].name',
'.pets[1].age'
]
});
// Retrieve the name and age of the second pet in the pets array.
let results = await client.json.get('noderedis:jsondata', {
path: [
'$.pets[1].name',
'$.pets[1].age'
]
});
// { '.pets[1].name': 'Rex', '.pets[1].age': 3 }
console.log(results);
// { '$.pets[1].name': [ 'Rex' ], '$.pets[1].age': [ 3 ] }
console.log(results);
// Goldie had a birthday, increment the age...
await client.json.numIncrBy('noderedis:jsondata', '.pets[2].age', 1);
results = await client.json.get('noderedis:jsondata', {
path: '.pets[2].age'
});
// Goldie had a birthday, increment the age...
await client.json.numIncrBy('noderedis:jsondata', '$.pets[2].age', 1);
results = await client.json.get('noderedis:jsondata', {
path: '$.pets[2].age'
});
// Goldie is 3 years old now.
console.log(`Goldie is ${JSON.stringify(results)} years old now.`);
// Goldie is 3 years old now.
console.log(`Goldie is ${JSON.stringify(results[0])} years old now.`);
// Add a new pet...
await client.json.arrAppend('noderedis:jsondata', '.pets', {
name: 'Robin',
species: 'bird',
isMammal: false,
age: 1
});
// Add a new pet...
await client.json.arrAppend('noderedis:jsondata', '$.pets', {
name: 'Robin',
species: 'bird',
isMammal: false,
age: 1
});
// How many pets do we have now?
const numPets = await client.json.arrLen('noderedis:jsondata', '.pets');
// How many pets do we have now?
const numPets = await client.json.arrLen('noderedis:jsondata', '$.pets');
// We now have 4 pets.
console.log(`We now have ${numPets} pets.`);
await client.quit();
}
managingJSON();
// We now have 4 pets.
console.log(`We now have ${numPets} pets.`);
await client.quit();