Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
Redis - How to find values with SREM?

I want to update my SET. I saw it's not possible, but I can do SREM and SADD again.

The problem is, I can't find the old value with SREM. My data is:

{"owner_id":30902468,"qu1":{"q1":"test","a1":"test"},"qu2":{"q2":"test","a2":"test"},"qu3":{"q3":"test","a3":"test"},"created_at":1648925484846,"score":1,"ip":"NA"}

I can get this with owner_id, so I did await redis.srem("qa", backtick{"owner_id":${owner_id}*backtick); However, it doesn't work.

Here is my full code:

import Redis from "ioredis";

export default async function create(req, res) {
  const redis = new Redis(process.env.U_REDIS_URL);
  const { q1, a1, q2, a2, q3, a3, owner_id } = req.body;

  if (!q1 && !a1) {
    res.status(400).json({
      error: "Type at least one question",
    });
  } else if (q1.length < 150) {
    const newEntry = {
      owner_id,
      qu1: { q1, a1 },
      qu2: { q2, a2 },
      qu3: { q3, a3 },
      created_at: Date.now(),
      score: 1,
      ip: "NA",
    };
    await redis.srem("qa", `{"owner_id":${owner_id}*`); // THIS IS NOT WORKING - DONT KNOW HOW TO GET OLD VALUE
    await redis.sadd("qa", JSON.stringify(newEntry));
    res.status(200).json({
      body: "success",
    });
  } else {
    res.status(400).json({
      error: "Max 150 characters please.",
    });
  }

  await redis.quit();
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The SREM command cannot be wildcarded. This simply won't work. To remove an item from a Set, you have to provide the entire member. So, for example:

> SADD foo apple
> SADD foo alpine
> SADD foo alpha

To replace apple with banana you cannot enter SREM foo ap* and then SADD foo banana. You have to match the entire member:

> SREM foo apple
> SADD foo banana

Based on your example, I think a better solution would be to store your JSON string in a String (or maybe RedisJSON if that's an option) with a key of qa:30902468 and then just store the owner IDs in the Set.

> SET qa:30902468 '{"owner_id":30902468,"qu1":{"q1":"test","a1":"test"},"qu2":{"q2":"test","a2":"test"},"qu3":{"q3":"test","a3":"test"},"created_at":1648925484846,"score":1,"ip":"NA"}'
> SADD qa 30902468

Then, when you need to change the string, you just change the String:

> SET qa:30902468 '{ "some": "other", "JSON": true }'

And when you need to read all the things in the Set, just ask the Set for the IDs:

> SMEMBERS qa
1) "30902468"
2) "30902469"
3) "30902470"

And then query the Strings based on the returned IDs:

> GET qa:30902468
> GET qa:30902469
> GET qa:30902470

What you're basically doing here is managing your own index using Sets in Redis.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda