无法筛选具有布尔值 true 的 ID,这些 ID 位于对象数组内。

huangapple go评论63阅读模式
英文:

Cannot filter the ids which have boolean value true inside the array of objects

问题

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 anyone help me.

Here is the image of my database schema:

无法筛选具有布尔值 true 的 ID,这些 ID 位于对象数组内。

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

英文:

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

无法筛选具有布尔值 true 的 ID,这些 ID 位于对象数组内。

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

答案1

得分: 1

以下是我翻译好的部分:

这是我找到的答案,为了获取特定记录,我使用了日期对象进行匹配,另外还有一个布尔值叫做 shouldBePaidByAdmin,如果为true则匹配,然后我将获得该记录的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);

// 为本月的起始日期创建一个新的日期对象
const startDate = new Date(date.getFullYear(), date.getMonth(), 1);

const endDate = new Date(date.getFullYear(), date.getMonth(), numDays);

// 获取符合条件的对象的ID

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",
            },
        },
    },
]);
英文:

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

得分: -1

Here is the translated code without the comments and additional content:

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));

I've removed the HTML entities and comments for clarity.

英文:
    const obj1 = {
_id: &quot;4fkdjvcdvdvdfv&quot;,
user: &quot;fcnjdjkriinc&quot;,
timeOfHolding: &quot;6months &quot;,
startedDate: &quot;2023-03-03&quot;,
isSchemeCompleted: false,
arr_object: [
{
amount: 5000,
deals: null,
date_paid: &quot;2023-05-11T18:30:00&quot;,
_id: &quot;3434fdvcvffkdccedmkn&quot;,
isPaymentComplete: true,
},
{
amount: 5000,
deals: null,
date_paid: &quot;2023-05-11T18:30:00&quot;,
_id: &quot;3434fdvcvffvfckedmkn&quot;,
isPaymentComplete: true,
},
{
amount: 5000,
deals: null,
date_paid: &quot;2023-05-11T18:30:00&quot;,
_id: &quot;3434fdvcvfcdffkedmkn&quot;,
isPaymentComplete: true,
},
{
amount: 5000,
deals: null,
date_paid: &quot;2023-05-11T18:30:00&quot;,
_id: &quot;3434fdvcvvfddffkedmkn&quot;,
isPaymentComplete: true,
},
{
amount: 5000,
deals: null,
date_paid: &quot;2023-05-11T18:30:00&quot;,
_id: &quot;3434fdvrvgrcvffkedmkn&quot;,
isPaymentComplete: false,
},
]
}
const obj2 = {
_id: &quot;4fkdjvcdvdvdfv&quot;,
user: &quot;fcnjdjkriinc&quot;,
timeOfHolding: &quot;6months &quot;,
startedDate: &quot;2023-03-03&quot;,
isSchemeCompleted: false,
arr_object: [
{
amount: 5000,
deals: null,
date_paid: &quot;2023-05-11T18:30:00&quot;,
_id: &quot;3434fdvcvffkdccedmkn&quot;,
isPaymentComplete: true,
},
{
amount: 5000,
deals: null,
date_paid: &quot;2023-05-11T18:30:00&quot;,
_id: &quot;3434fdvcvffvfckedmkn&quot;,
isPaymentComplete: false,
},
{
amount: 5000,
deals: null,
date_paid: &quot;2023-05-11T18:30:00&quot;,
_id: &quot;3434fdvcvfcdffkedmkn&quot;,
isPaymentComplete: true,
},
{
amount: 5000,
deals: null,
date_paid: &quot;2023-05-11T18:30:00&quot;,
_id: &quot;3434fdvcvvfddffkedmkn&quot;,
isPaymentComplete: true,
},
{
amount: 5000,
deals: null,
date_paid: &quot;2023-05-11T18:30:00&quot;,
_id: &quot;3434fdvrvgrcvffkedmkn&quot;,
isPaymentComplete: false,
},
]
}
const filterArr = (arr) =&gt; {
let tempArr = [];
let count = 0;
for (let i=0; i&lt;arr?.arr_object?.length; i++) {
const obj = arr?.arr_object;
if (!obj[i].isPaymentComplete) count++;
}
if (count === 1 &amp;&amp; &amp;&amp;
arr?.arr_object?.[arr?.arr_object?.length - 1].isPaymentComplete === false
)
{
tempArr = arr?.arr_object?.map((obj) =&gt; 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) =&gt; {
let count = 0;
for (let i = 0; i &lt; arr?.arr_object?.length; i++) {
const obj = arr?.arr_object;
if (!obj[i].isPaymentComplete) count++;
}
if (
count === 1 &amp;&amp;
arr?.arr_object?.[arr?.arr_object?.length - 1].isPaymentComplete === false
)
return arr?.user;
else return &quot;&quot;;
};

huangapple
  • 本文由 发表于 2023年5月15日 13:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定