Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!