skip to Main Content

I am trying to filter those records ids that have isPaymentCompleted: true inside the arr_object but the condition is that the last object’s boolean value must be false. All the other objects’ boolean values must be actual if there is another false value inside the arr_object except the last one then that record’s id won’t be selected.
Is there any problem understanding my query please comment

I’ve tried multiple times but still, I can’t figure out how to solve this problem. Please any one help me

Here is the image of my database schema

enter image description here

I want only record ids, not the inside array object ids.

2

Answers


  1. Chosen as BEST ANSWER

    Here is the answer I figured to actually get that particular record I use the date object to match and another boolean called the shouldBePaidByAdmin object should true match then I will get the id

    const year = new Date().getFullYear();
        const month = new Date().getMonth() + 1;
    
        const numDays = new Date(year, month, 0).getDate();
    
        const monthNames = [
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December",
        ];
    
        const date = new Date(year, month - 1);
    
        // Create a new Date object for the start of the month
        const startDate = new Date(date.getFullYear(), date.getMonth(), 1);
    
        const endDate = new Date(date.getFullYear(), date.getMonth(), numDays);
    
        //Get the ids of those objects that matches the condition
    
        const result = await PaymentScheme.aggregate([
            {
                $match: {
                    paymentData: {
                        $elemMatch: {
                            dueDate: {
                                $gte: startDate,
                                $lt: endDate,
                            },
                            shouldBePaidByAdmin: true,
                            isPaymentDone: false,
                        },
                    },
                },
            },
            {
                $unwind: "$paymentData",
            },
            {
                $match: {
                    "paymentData.dueDate": {
                        $gte: startDate,
                        $lt: endDate,
                    },
                    "paymentData.shouldBePaidByAdmin": true,
                    "paymentData.isPaymentDone": false,
                },
            },
            {
                $group: {
                    _id: "$user",
                    userIds: {
                        $push: "$_id",
                    },
                },
            },
        ]);
    

  2.     const obj1 = {
          _id: "4fkdjvcdvdvdfv",
          user: "fcnjdjkriinc",
          timeOfHolding: "6months ",
          startedDate: "2023-03-03",
          isSchemeCompleted: false,
          arr_object: [
            {
              amount: 5000,
              deals: null,
              date_paid: "2023-05-11T18:30:00",
              _id: "3434fdvcvffkdccedmkn",
              isPaymentComplete: true,
            },
            {
              amount: 5000,
              deals: null,
              date_paid: "2023-05-11T18:30:00",
              _id: "3434fdvcvffvfckedmkn",
              isPaymentComplete: true,
            },
            {
              amount: 5000,
              deals: null,
              date_paid: "2023-05-11T18:30:00",
              _id: "3434fdvcvfcdffkedmkn",
              isPaymentComplete: true,
            },
            {
              amount: 5000,
              deals: null,
              date_paid: "2023-05-11T18:30:00",
              _id: "3434fdvcvvfddffkedmkn",
              isPaymentComplete: true,
            },
            {
              amount: 5000,
              deals: null,
              date_paid: "2023-05-11T18:30:00",
              _id: "3434fdvrvgrcvffkedmkn",
              isPaymentComplete: false,
            },
          ]
        }
        
        const obj2 = {
          _id: "4fkdjvcdvdvdfv",
          user: "fcnjdjkriinc",
          timeOfHolding: "6months ",
          startedDate: "2023-03-03",
          isSchemeCompleted: false,
          arr_object: [
            {
              amount: 5000,
              deals: null,
              date_paid: "2023-05-11T18:30:00",
              _id: "3434fdvcvffkdccedmkn",
              isPaymentComplete: true,
            },
            {
              amount: 5000,
              deals: null,
              date_paid: "2023-05-11T18:30:00",
              _id: "3434fdvcvffvfckedmkn",
              isPaymentComplete: false,
            },
            {
              amount: 5000,
              deals: null,
              date_paid: "2023-05-11T18:30:00",
              _id: "3434fdvcvfcdffkedmkn",
              isPaymentComplete: true,
            },
            {
              amount: 5000,
              deals: null,
              date_paid: "2023-05-11T18:30:00",
              _id: "3434fdvcvvfddffkedmkn",
              isPaymentComplete: true,
            },
            {
              amount: 5000,
              deals: null,
              date_paid: "2023-05-11T18:30:00",
              _id: "3434fdvrvgrcvffkedmkn",
              isPaymentComplete: false,
            },
          ]
        }
        
        const filterArr = (arr) => {
          let tempArr = [];
          let count = 0;
          for (let i=0; i<arr?.arr_object?.length; i++) {
            const obj = arr?.arr_object;
            if (!obj[i].isPaymentComplete) count++;
          }
      
    
        if (count === 1 && &&
            arr?.arr_object?.[arr?.arr_object?.length - 1].isPaymentComplete === false
          )
    
     {
            tempArr = arr?.arr_object?.map((obj) => obj._id);
          }
          return tempArr;
        }
        
        console.log(filterArr(obj1));
        console.log(filterArr(obj2));
    
        // In case you want user id the filterArr function would be like this
        
      
    
          const filterArr = (arr) => {
          let count = 0;
          for (let i = 0; i < arr?.arr_object?.length; i++) {
            const obj = arr?.arr_object;
            if (!obj[i].isPaymentComplete) count++;
          }
          if (
            count === 1 &&
            arr?.arr_object?.[arr?.arr_object?.length - 1].isPaymentComplete === false
          )
            return arr?.user;
          else return "";
        };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search