#77 Sorting result | Using MongoDB with Express| A Complete NODE JS Course

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 พ.ย. 2024

ความคิดเห็น • 53

  • @tanomenossorvas5270
    @tanomenossorvas5270 11 หลายเดือนก่อน

    Great content

  • @acupoflie
    @acupoflie 11 หลายเดือนก่อน +5

    Fixed Mongoose problem
    add
    delete queryObj.sort
    before the
    query = Movie.find(queryObj)
    it would work for someones who have the problem with this, this solve is for Mongoose 7.0 or later

    • @taiwotopesunday
      @taiwotopesunday 9 หลายเดือนก่อน

      This is working fine now. Both filters and sorting are working together.

    • @midunc5317
      @midunc5317 4 หลายเดือนก่อน

      what is the reason?

    • @acupoflie
      @acupoflie 4 หลายเดือนก่อน

      @@midunc5317 mongoose have to do it automatically but after 7.0 you have to it manually

  • @RohithAppala-p1k
    @RohithAppala-p1k 7 หลายเดือนก่อน

    Good explanation

  • @Ranta129
    @Ranta129 ปีที่แล้ว +6

    Sir i want to ask, when i use queryObj as a parameter in Movie.find() method i cant sort the movie and it will return an empty object in postman. Is it not possible to have sort parameter and other parameter such as duration etc in 1 request? Im using mongoose 7.1.1 version btw

    • @procademy
      @procademy  ปีที่แล้ว

      For such cases i have mentioned two solutions in the filter lecture. You can use that solution where we are removing other query parameters like sort field etc. To make the filter code work

    • @anxhelocenollari4269
      @anxhelocenollari4269 ปีที่แล้ว +1

      i saw that but we want to have the logic for both implemented. So if we use the filter in the filter section does it mean that we cannot do the sorting? and otherwise?@@procademy

    • @pradeepgoud1436
      @pradeepgoud1436 8 หลายเดือนก่อน +2

      Yes bro am also facing same problem

    • @leapthree8491
      @leapthree8491 7 หลายเดือนก่อน

      Same here. Were you able to find a solution?@@pradeepgoud1436

    • @BertrandEkong
      @BertrandEkong 20 วันที่ผ่านมา

      @@procademy but mine too is not working i use mongoose verison 8

  • @hassannouri9796
    @hassannouri9796 3 หลายเดือนก่อน

    ❤❤❤

  • @vinayvallychaladi6193
    @vinayvallychaladi6193 ปีที่แล้ว +1

    Thanks for the vedios...very informative...I am not getting search results after sorting
    Response returns status as success but movies is empty.

    • @michelnunes4421
      @michelnunes4421 ปีที่แล้ว +3

      I'm having the same problem, all filters are working just fine but sort always returns a empty object. I have tried console.log() every step of the code and every thing seems to be working just fine. I can't figure out what is happening. If you find a fix please let me know.

    • @awesomeguy6427
      @awesomeguy6427 ปีที่แล้ว

      @@michelnunes4421 fixed it

    • @006daredevil
      @006daredevil ปีที่แล้ว

      @@awesomeguy6427 Can you share the code? How did you fix it?

    • @infotake
      @infotake ปีที่แล้ว

      ,@@awesomeguy6427 how do you fix it?

    • @mozammilahmad8431
      @mozammilahmad8431 ปีที่แล้ว

      I fixed it

  • @usmanrangrez-cd7zj
    @usmanrangrez-cd7zj 8 หลายเดือนก่อน

    Explanation with comments
    Clean:
    const filterMovie = async (req, res) => {
    try {
    const excludeFields = ["sort", "page", "limit", "fields"];
    let queryObj = { ...req.query };
    excludeFields.forEach((field) => {
    delete queryObj[field];
    });
    let queryStr = JSON.stringify(queryObj);
    queryStr = queryStr.replace(/\b(gte|gt|lte|lt)\b/g, (match) => {
    return `$${match}`;
    });
    queryObj = JSON.parse(queryStr);
    let query = Movie.find(queryObj);
    if (req.query.sort) {
    const sortCriteria = req.query.sort.split(",").join(" ");
    query = query.sort(sortCriteria);
    } else {
    query = query.sort({ createdAt: -1 });
    }
    const movies = await query;
    if (movies.length === 0) {
    return res.status(404).json({
    message: "No movies match your criteria!",
    });
    }
    res.status(200).json({
    status: "success",
    data: {
    movies,
    },
    });
    } catch (error) {
    res.status(500).json({
    status: "fail",
    message: error.message,
    });
    }
    };
    Check Reply for commented one

  • @muhangielioda
    @muhangielioda ปีที่แล้ว

    how do we implement where as type in the form then it keeps bring results of search typing words

  • @bhargaviroyal5866
    @bhargaviroyal5866 11 หลายเดือนก่อน

    I fixed it for version 7 , version 8
    let query=Movie.find(queryObj);
    let query1=Movie.find();
    if(req.query.sort){
    query=query1.sort(req.query.sort);
    }
    const movies=await query;

    • @RHILL-hb1hr
      @RHILL-hb1hr 9 หลายเดือนก่อน

      thanks!

    • @ThangaBujji
      @ThangaBujji 4 หลายเดือนก่อน

      Thanks you.. It working

  • @abdosamy8981
    @abdosamy8981 ปีที่แล้ว +1

    sort not working. returning an empty movies array. Any Fix?

    • @procademy
      @procademy  ปีที่แล้ว

      Are you using mongoose 7 version? Might be because of version difference.

    • @abdosamy8981
      @abdosamy8981 ปีที่แล้ว +1

      @@procademy Yes, I'm using version 7. And I figured out what was wrong. I needed to remove the field from the queryObj before I pass it to the query.

    • @rishabhrishabh3804
      @rishabhrishabh3804 ปีที่แล้ว

      @@abdosamy8981 can you pls tell how

    • @rishabhrishabh3804
      @rishabhrishabh3804 ปีที่แล้ว +1

      @@abdosamy8981 But then filtering method dont work

    • @taiwotopesunday
      @taiwotopesunday 9 หลายเดือนก่อน

      Am using version 8, but sort() still returns an empty array. How so I can fix this, please?

  • @BertrandEkong
    @BertrandEkong 20 วันที่ผ่านมา

    it is returning an empty string

  • @saisachidanandasahoo5769
    @saisachidanandasahoo5769 8 หลายเดือนก่อน +2

    //It should work
    let queryStr = JSON.stringify(req.query);
    queryStr = queryStr.replace(/\b(gte|gt|lte|lt)\b/g, (match) => `$${match}`);
    const queryObj = JSON.parse(queryStr);
    // Remove 'sort' from queryObj
    if(req.query.sort) {
    delete queryObj.sort;
    }
    let query = Movie.find(queryObj);
    if(req.query.sort) {
    query = query.sort(req.query.sort);
    }
    const movies = await query;

  • @BertrandEkong
    @BertrandEkong 20 วันที่ผ่านมา

    yes sir i get error when sorting

  • @laluprasad3775
    @laluprasad3775 ปีที่แล้ว

    Bro complete node js ,i think it is end to volpl😮

  • @Baig_Sahab00
    @Baig_Sahab00 ปีที่แล้ว

    bro sort method is not working in my code that to everything is perfect.

    • @mozammilahmad8431
      @mozammilahmad8431 ปีที่แล้ว

      I fixed it

    • @abdosamy8981
      @abdosamy8981 ปีที่แล้ว

      @@mozammilahmad8431 How?

    • @csais2472
      @csais2472 ปีที่แล้ว +1

      @@abdosamy8981 Just erase queryObj from the query variable params because queryObj is giving the whole request object and not the actual query from database. Should be like this let query = Movie.find();

    • @akhilp3826
      @akhilp3826 ปีที่แล้ว

      @@csais2472 worked that worked thanks

    • @anxhelocenollari4269
      @anxhelocenollari4269 ปีที่แล้ว

      but does it mean that we lost the abbility to find by different parameters other than sort?@@csais2472

  • @mozammilahmad8431
    @mozammilahmad8431 ปีที่แล้ว +1

    my sort method not works
    try {
    let queryStr=JSON.stringify(req.query);
    queryStr=queryStr.replace(/\b(gte|gt|lte|lt)\b/g,(match)=>`$${match}`);
    let queryObj=JSON.parse(queryStr);

    let query = Movie.find(queryObj)

    if(req.query.sort){
    query=query.sort(req.query.sort)

    }
    const movies=await query;
    //THIS LOG [ ] EMPTY ARRAY
    console.log(movies)
    res.status(200).json({
    status: "success",
    length: movies.length,
    data: {
    movies: movies,
    },
    });
    } catch (err) {
    res.status(404).json({
    status: "fail",
    message: err.message,
    });
    }
    };