Abdelrahman Shamia
Abdelrahman Shamia

Reputation: 43

How to get data with a cleaner way using mongoose?

I'm filtering the data based on a Boolean savedBoolean , and if that Boolean is not being inputted I'm getting all the data, this code works for now. But how to do it in a cleaner way since I'm duplicating the code.


let filteredReviews : any | undefined;
  if (savedBoolean === true || savedBoolean === false) {
    filteredReviews = await Interviewee.aggregate([{
      $project: {
        _id: 0,
        userId: 1,
        'interviews.review': 1,
      },
    },

    {
      $unwind: '$interviews',
    },
    {
      $match: {
        userId: '4',
        'interviews.review.saved': savedBoolean,
      },
    },
    {
      $group: {
        _id: '$interviews.review._id',
        review: {
          $first: '$interviews.review',
        },
      },
    },
    ]).skip((Number(page) - 1) * 3).limit(3);
  }

  if (savedBoolean === undefined) {
    filteredReviews = await Interviewee.aggregate([{
      $project: {
        _id: 0,
        userId: 1,
        'interviews.review': 1,
      },
    },
    {
      $match: {
        userId: '4',
      },
    },
    {
      $unwind: '$interviews',
    },

    ]).skip((Number(page) - 1) * 3).limit(3);
  }

Upvotes: 1

Views: 181

Answers (1)

Binni kumari
Binni kumari

Reputation: 358

In MongoDB, the db.collection.remove() method removes documents from a collection. You can remove all documents from a collection, remove all documents that match a condition, or limit the operation to remove just a single document.

Upvotes: 1

Related Questions