0

Originally I was trying to add the documents returned from User.find({}) into the redis cache as a sorted set so that I can do pagination. like so: client.zadd("directory", documents_id, docsString, (err, reply) => {

I'm starting to wonder if I need to use hmset since I'm dealing with multiple items in JSON object, but I'm not sure how to query if that's the case. I'm looking for the best solution that will later allow me to paginate the data and also have a keyword that returns matching values in fields (if possible to any extent with redis)

app.get("/api/users", (req, res, next) => {

     var searchKey = new RegExp(req.query.usernameSearch, "i");

    console.log("CURRENT PAGE IS " + req.query.currentPage);
    client.zrange("directory", 0, -1, (err, reply) => {
      console.log("reply");
      console.log(reply);

      console.log(err);

      if (err) {
        res.status(401).json({
          message: "Directory Cache Error!"
        });
        return;
      } else if (reply) {
        console.log("GRABBING FROM CACHE");
        let packageReply = JSON.parse(reply);
        console.log("packageReply");
        console.log(packageReply);
        let totatArtists = packageReply.length;
        res.status(200).json({
          message: "Grabbed from cache",
          posts: packageReply,
          maxPosts: totatArtists
        });
      } else {
        console.log("GRABBING FROM DB");

        User.countDocuments({
          $and: [{ username: searchKey }, { role: "Seller" }]
        }).then(docs => {
          let totalPosts = docs;
          let postPerPage = 20;
          User.find({
            $and: [{ username: searchKey }, { role: "Seller" }]
          })
            .select("_id username")
            .skip(postPerPage * (currentPage - 1))
            .limit(postPerPage)
            .then(documents => {
              let docsString = JSON.stringify(documents);
              client.zadd(
                "directory",
                documents_id,
                docsString,
                (err, reply) => {
                  client.expire("directory", 3600);

                  res.status(200).json({
                    message: "Users retrieved successfully!",
                    posts: documents
                  });
                }
              );
            });
        });
      }
    });

sample output

 [{ _id: 5e34482ce04d7c0ca4725f92, username: 'bob' },
  { _id: 5e344842e04d7c0ca4725f93, username: 'joe' },
  { _id: 5e383e5dace65e4774e646e1, username: 'bill' },
  { _id: 5e383e63ace65e4774e646e2, username: 'sue' }]
user6680
  • 79
  • 6
  • 34
  • 78

1 Answers1

0

The client.zadd("directory", ... function has parameters:

  1. The first is the key of the Z set
  2. The second is score (number)
  3. The third is new value

Assuming document ID is the score you want to use to later retrieve, you should do:

client.zadd("directory", documents._id, docsString, ...
LeoMurillo
  • 6,048
  • 1
  • 19
  • 34
  • The goal was to make document._id the user's id I'm going to need to reference later when retrieving specific data. I have a search too that lets me search the user in mongodb and it updates the listings after they're done typing, so I'm gonna want it to eventually make that functionality work with redis cache so I need to be able to query that data later. I'm trying to add all documents at once so ```documents``` that's JSON.stringified into ```docsString``` could be made up of 100s of rows like this example```[{"_id": "342342342dsffsdfasfdsf", "username": "bob"}]``` – user6680 Feb 11 '20 at 19:33
  • Also I got rid of error above, but zrange keeps returning null so it skips the cache and hits the db everytime so I don't know if I'm storing the data in redis correctly. Updated code: https://pastebin.com/kLPxCvnT – user6680 Feb 11 '20 at 20:02