【问题标题】:How to change this response to simple array?如何将此响应更改为简单数组?
【发布时间】:2020-06-17 07:22:59
【问题描述】:
{
"success": true,
"message": "Result",
"data": [
    {
        "Here": [
           {
                "_id": "5ee97ee7f25d1c1482717bdf",
                "email": "test1@test.io",
                "profileImages": [],
                "username": "test1",
                "birthday": "2020-06-11T10:11:32.000Z",
                "phoneNumber": "+910000000000",
                "location": "Test Location",
                "firstName": "test1",
                "lastName": "test1",

            }
        ]
    },
    {
        "Here": [
           {
                "_id": "5ee97ef2f25d1c1482717be1",
                "email": "test2@test.io",
                "profileImages": [],
                "username": "test2",
                "birthday": "2020-06-11T10:11:32.000Z",
                "phoneNumber": "+910000000000",
                "location": "Test Location"
            }

        ]
    }
],

}

我期待的是这个

 {
"success": true,
"message": "Result",
data: [
       {
            "_id": "5ee97ee7f25d1c1482717bdf",
            "email": "test1@test.io",
            "profileImages": [],
            "username": "test1",
            "birthday": "2020-06-11T10:11:32.000Z",
            "phoneNumber": "+910000000000",
            "location": "Test Location",
            "firstName": "test1",
            "lastName": "test1"},
        {
            "_id": "5ee97ef2f25d1c1482717be1",
            "email": "test2@test.io",
            "profileImages": [],
            "username": "test2",
            "birthday": "2020-06-11T10:11:32.000Z",
            "phoneNumber": "+910000000000",
            "location": "Test Location"
        }
    ]

}

我正在使用的查询是针对此响应,在 mongodb、查找和项目中使用聚合,这导致我得到一些不希望的响应

db.collections.aggregate( [

            {
            $lookup: {
                from: 'users',
                as: 'Here',
                let: {
                    whoDid: '$whoDid'
                },
                pipeline: [
                    {
                        "$match": { "$expr": { "$eq": ["$_id", "$$whoDid"] } }
                    },
                    {
                                $project: {
                                _id: 1,
                                email: 1,
                                profileImages: 1,
                                username: 1,
                                birthday: 1,
                                phoneNumber: 1,
                                firstName: 1,
                                lastName: 1,
                                fullName: 1,

                                // age: {$year: "$birthday"} 
                                age: {
                                    $divide: [{ $subtract: [new Date(), "$birthday"] },
                                    (31558464000)]
                                }
                            }
                    }
                ],
            }

        },

            {
                $project:{
                    Here:1,
                    _id:0

                }
            }  ,   
        ])

who did table 是我存储用户 ID 的集合之一,后来我使用查找填充数据

{
"_id" : ObjectId("5ee988eb1aac0022e15dbb7b"),
"whoDid" : ObjectId("5ee97ef2f25d1c1482717be1"),
"toWhomDid" : ObjectId("5ee97ec0f25d1c1482717bdd"),
"modified_at" : ISODate("2020-06-17T03:07:23.217Z"),
"created_at" : ISODate("2020-06-17T03:07:23.217Z"),
"__v" : 0
}
{
"_id" : ObjectId("5ee988eb1aac0022e15dbb7c"),
"whoDid" : ObjectId("5ee97ec0f25d1c1482717bdd"),
"toWhomDid" : ObjectId("5ee97ef2f25d1c1482717be1"),
"modified_at" : ISODate("2020-06-17T03:07:23.220Z"),
"created_at" : ISODate("2020-06-17T03:07:23.220Z"),
"__v" : 0
}

谁能给我建议更好的选择,以便我得到想要的反应?

【问题讨论】:

    标签: node.js arrays mongodb collections


    【解决方案1】:

    可以使用reduce方法:

    obj.data = obj.data.reduce((a, c) => {
        a.push(...c.Here);
        return a;
    }, [])
    

    一个例子:

    let obj = {
      "success": true,
      "message": "Result",
      "data": [      {
              "Here": [             {
                      "_id": "5ee97ee7f25d1c1482717bdf",                  "email": "test1@test.io",
                      "profileImages": [],                  "username": "test1",
                      "birthday": "2020-06-11T10:11:32.000Z",                  "phoneNumber": "+910000000000",                  "location": "Test Location",
                      "firstName": "test1",                  "lastName": "test1",
                  }
              ]
          },
          {
              "Here": [             {
                      "_id": "5ee97ef2f25d1c1482717be1",
                      "email": "test2@test.io",
                      "profileImages": [],
                      "username": "test2",
                      "birthday": "2020-06-11T10:11:32.000Z",
                      "phoneNumber": "+910000000000",
                      "location": "Test Location"
                  }
              ]
          }
      ]
    };
    
    
    obj.data = obj.data.reduce((a, c) => {
        a.push(...c.Here);
        return a;
    }, [])
    console.log(obj);

    【讨论】:

      【解决方案2】:

      将这些额外步骤添加到您的聚合管道中:

      {
        $unwind: "$Here"
      },
      {
        $replaceWith: "$Here"
      }
      

      MongoPlayground

      注意:您可以将$project: { _id: 1, email: 1, ...替换为:

      {
        $addFields:{
          age: {
            $divide: [{ $subtract: [new Date(), "$birthday"] },(31558464000)]
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-19
        • 2021-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多