【问题标题】:How do i use $project and $sort here?我如何在这里使用 $project 和 $sort ?
【发布时间】:2023-01-25 10:30:32
【问题描述】:

我正在尝试将我的数据从我的网络应用程序导出到 Excel 工作表。我在 node js 中编写 next js 和 mongodb 是数据库。我正在从我的数据库中获取数据并将其运行到我的应用程序中,然后制作一张表格。该表似乎是按日期时间组织的。我希望它按名字或姓氏组织,然后当它导出到 excel 时它显示 _id 字段。它没有在我的应用程序中显示 _id 字段,但我认为是因为我在应用程序中没有列,但为什么它显示在我的 excel 工作表中。我是一名新开发人员,我刚刚从以前的开发人员那里接手了这个应用程序,没有一个老开发人员离开了。

我试图添加 $project 等,但是当我更改文件时我什么也没做,似乎没有任何作用。我能找到的唯一一个文件,它看起来像是在处理数据,如下所示:

export default async function handler(req,res){
    //dbConnect(req);
    let db = await dbConnect(req);

    const Persons_Type_Information = db.models.Persons_Type_Information || db.model("Persons_Type_Information",personTypesInformationSchema)
    
    const {method} = req;
    const {rolecd} = req.query;

    switch (method){
        case "GET":
            let resp = [];
            try{
                let responses = await Persons_Type_Information.aggregate([
                    { $match: {
                        $expr: {
                          $and: [
                            {$eq: [{
                              $dateToString: {
                                date: {
                                  $subtract: [
                                    new Date(),
                                    0
                                  ]
                                },
                                format: '%Y-%m-%d'
                              }
                            },
                              {$dateToString: {
                                date: '$pti_signed_in_date',
                                format: '%Y-%m-%d'
                              }
                            }]},
                            {$or: [
                              {$eq: [
                                "$pt_type",
                                "Employee"
                              ]},
                              {$eq: [
                                "$pt_type",
                                "Contractor"
                              ]}
                            ]}
                          ]
                        },
                      } },
                    { $group: 
                        {
                            _id: {$cond: {
                              'if': {
                                $eq: [
                                  '$pti_visiting_location',
                                  ''
                                ]
                              },
                              then: 'Employee',
                              'else': '$pti_visiting_location'
                            }},
                            count: {
                              $sum: {
                                $cond: {
                                  'if': {
                                    $ne: [
                                      '$pti_signed_in_date',
                                      ''
                                    ]
                                  },
                                  then: 1,
                                  'else': 0
                                }
                              }
                            }
                            
                          
                            
                          }, 
                    }
                    ]);
                                     
                return res.json({
                //    data: JSON.parse(JSON.stringify(resp)),
                    data : JSON.parse(JSON.stringify(responses)),
                    success: true
                    
                }),
                console.log(res);
                
            }catch(error){
                return res.send({
                    data : new Error(error).message,
                    success: false
                })
            }

        default:

我找错地方了吗?我觉得这是正确的地方

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    这不是答案。

    保持冷静@Mimir,听到你一个人的消息很难过。

    1. 深吸一口气。
    2. 与您的经理谈谈您需要时间学习。
    3. 看看MongoDB Official Nodejs Learning Path。

      在您获得足够的知识后,其他可能的方法来解决您的问题。

      • 为此方法编写单元测试。
      • 下载 MongoDB Compass 并使用它进行逐步聚合。

      任何方式回答你的问题为什么 _id 没有出现是由于 $group 阶段。

      祝你好运,你需要它。

    【讨论】:

      猜你喜欢
      • 2018-05-09
      • 2019-06-15
      • 2021-10-25
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-16
      相关资源
      最近更新 更多