prismaaggregateRowmongodb中的created_at搜索不起作用

问题描述 投票:0回答:1

我使用

date-fns
库来处理日期。代码提供了他们的方法。

prisma: 5.22.0

@prisma/client: 5.22.0

async getAllOnYear() {
    const date = new Date()
    const startYear = startOfYear(date)
    const endYear = endOfYear(date)

    console.log('Start Year:', startYear)
    console.log('End Year:', endYear)

    const monthlyStatisticsRaw = await this.prisma.productRelease.aggregateRaw({
        pipeline: [
            {
            $match: {
                marking: { $ne: EnumProductReleaseMarking.deleted },
                created_at: {
                    $gte: startYear,
                    $lte: endYear
                }
            }
        },
        {
            $group: {
                _id: {
                    month: { $month: '$created_at' }
                },
                totalAmount: { $sum: '$total_amount' },
                totalSale: { $sum: '$total_sale' },
                totalSwap: { $sum: '$total_swap' },
                totalBonus: { $sum: '$total_bonus' },
                count: { $sum: 1 }
            }
            }
        ]
    })

    console.log('raw', monthlyStatisticsRaw)
}

控制台:

Start Year: 2023-12-31T19:00:00.000Z 
End Year: 2024-12-31T18:59:59.999Z 
raw []

棱镜型号:

model ProductRelease {
    id        String   @id @default(auto()) @map("_id") @db.ObjectId
    createdAt DateTime @default(now()) @map("created_at")
    updatedAt DateTime @updatedAt @map("updated_at")

    user          User?   @relation(fields: [userId], references: [id])
    userId        String? @map("user_id") @db.ObjectId
    
    tag     String                    @unique
    status  EnumProductReleaseStatus
    marking EnumProductReleaseMarking @default(null)
    
    totalAmount Float @map("total_amount")
    totalSale   Int   @map("total_sale")
    totalSwap   Int   @map("total_swap")
    totalBonus  Int   @map("total_bonus")
    
    @@map("product_release")

}

我尝试了不同的方法将日期转换为 toISOString() 等。我尝试将created_at更改为createdAt,但没有结果。

如果删除created_at,一切都会正常:

Start Year: 2023-12-31T19:00:00.000Z
End Year: 2024-12-31T18:59:59.999Z
raw [
     {
        _id: { month: 11 },
        totalAmount: 345664,
        totalSale: 336,
        totalSwap: 50,
        totalBonus: 106,
        count: 10
     }
]

为了完成图片,我还根据要求扔掉了数据:

await this.prisma.productRelease.aggregateRaw({
    pipeline: [
        {
            $project: { created_at: 1, marking: true }
        },
        {
            $sort: { created_at: 1 }
        }
    ]
})

结果:

 {
        _id: { '$oid': '673c955783744a1eaf4e52ee' },
        created_at: { '$date': '2024-11-19T13:40:38.298Z' },
        marking: 'accounting'
 },
 {
        _id: { '$oid': '673e5083a9570d92a284c2b7' },
        created_at: { '$date': '2024-11-20T21:11:30.742Z' },
        marking: 'null'
 },
 {
       _id: { '$oid': '673f9542f9388c92967b84a8' },
        created_at: { '$date': '2024-11-21T20:17:05.329Z' },
        marking: 'null'
 },

经过20次尝试,我通过反复试验解决了这个问题,我能够首先将工作方法输出到$expr,然后找到通常的$match的方法。解决办法如下:

await this.prisma.productRelease.aggregateRaw({
    pipeline: [
        {
            $match: {
                marking: { $ne: EnumProductReleaseMarking.deleted },
                created_at: {
                    $gte: { $date: startYear },
                    $lte: { $date: endYear }
                }
            }
        },
        {
            $group: {
                _id: {
                    month: { $month: '$created_at' }
                },
                totalAmount: { $sum: '$total_amount' },
                totalSale: { $sum: '$total_sale' },
                totalSwap: { $sum: '$total_swap' },
                totalBonus: { $sum: '$total_bonus' },
                count: { $sum: 1 }
            }
        }
    ]
})
mongodb aggregate prisma nest date-fns
1个回答
0
投票

经过 20 次尝试,我通过反复试验解决了这个问题,我能够首先将工作方法输出到 $expr,然后找到通常的 $match 的方法。解决办法如下:

await this.prisma.productRelease.aggregateRaw({
    pipeline: [
        {
            $match: {
                marking: { $ne: EnumProductReleaseMarking.deleted },
                created_at: {
                    $gte: { $date: startYear },
                    $lte: { $date: endYear }
                }
            }
        },
        {
            $group: {
                _id: {
                    month: { $month: '$created_at' }
                },
                totalAmount: { $sum: '$total_amount' },
                totalSale: { $sum: '$total_sale' },
                totalSwap: { $sum: '$total_swap' },
                totalBonus: { $sum: '$total_bonus' },
                count: { $sum: 1 }
            }
        }
    ]
})
© www.soinside.com 2019 - 2024. All rights reserved.