0

i'm struggling with mongoDB API, This is my representation of my data :

I'd like to get one list that is stored in userlists correpsonding to his title

Basicaly, i want to fetch only one data that corresponds to a userID and a listTitle

This is my current request and this is not working

exports.getOneMoviesListOfUser = (req, res) => {
  const uid = req.params.uid;
  const titrelist = req.params.titrelist;

  if(uid && titrelist) {
    var condition = {"uid": {$regex : new RegExp(uid), $options: "i"}, "userlists.titrelist": {$regex : new RegExp(titrelist), $options: "i"}}
  } else {
    var condition = {}
  }

  MoviesDB.find(condition)
    .then((data) => {
      if (!data)
        res
          .status(404)
          .send({
            message:
              "Liste de films non trouvée " + uid + "liste = " + titrelist,
          });
      else res.send(data);
    })
    .catch((err) => {
      res
        .status(500)
        .send({
          message:
            "Erreur pendant la récupération de la liste " +
            uid +
            "liste = " +
            titrelist,
        });
    });
};



Ty if you can help me with this.

  • Can you add more detail on what you mean by "not working". I would expect your query to return the whole document, not just the array item you demonstrate - is that what you mean? Additionally doing a $regex lookup on the uid is probably not necessary - and will definitely slow this query down. – AdamExchange Oct 28 '22 at 11:31
  • When i say not working, it means that the request is sending me the lists of the user but i'm looking for one list instead of the whole list. Then i tried this When i try simple request like that { uid: uid, "userlists.titrelist": titrelist }, { $match: { "userlists.$.titrelist": { titreliste: titrelist }, }, } but it makes the api loading like if i made an infinite loop. – Geoffrey Servant Oct 30 '22 at 18:25
  • Does this answer your question? [Return only matched sub-document elements within a nested array](https://stackoverflow.com/questions/36229123/return-only-matched-sub-document-elements-within-a-nested-array) – AdamExchange Oct 30 '22 at 19:00
  • I tried with this example but it still load infinitly. idk why but it loads when i iterate into arrays of userlists. – Geoffrey Servant Oct 30 '22 at 19:35

0 Answers0