英文:
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:
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
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: "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 "";
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论