【问题标题】:How to make projection to display limited data如何进行投影以显示有限的数据
【发布时间】:2020-08-19 09:56:51
【问题描述】:

如何在响应中仅显示有限的数据而不是整个对象

  {
        "message": "Records",
        "data": [
            {
                "_id": "5f3ccf2448e5181dc80a70d3",
                "string": "string",
                "records": [
                    {
                        "_id": "5f3ccfc35417491564bbe2z9",
                        "product_id": "5ecd3a93f1314915404xd6ds",
                        "my_detail": {
                            "_id": "5ecc00f97c96f408384d57c4",
                            "active": 0,
                            "status": "pending",
                            "deleted_at": 0,
                            "email": "records@yopmail.com",
                            "password": "$2a$12$7h3mttR8TUlZZKIzQvnVV.1sdfsd56.sdfsdfBOxA6UBkpRBLFVC",
                            "first_name": "Johny",
                            "last_name": "Hales",
                            "createdAt": "2020-05-25T17:31:37.016Z",
                            "updatedAt": "2020-05-25T17:31:37.016Z",
                        }
                    },
                    {
                        "_id": "5f3ccfc35417491564bbe2z9",
                        "product_id": "5ecd3a93f1314915404xd6ds",
                        "my_detail": {
                            "_id": "5ecc00f97c96f408384d57c4",
                            "active": 0,
                            "status": "pending",
                            "deleted_at": 0,
                            "email": "records@yopmail.com",
                            "password": "$2a$12$7h3mttR8TUlZZKIzQvnVV.1sdfsd56.sdfsdfBOxA6UBkpRBLFVC",
                            "first_name": "Steve",
                            "last_name": "harvey",
                            "createdAt": "2020-05-25T17:31:37.016Z",
                            "updatedAt": "2020-05-25T17:31:37.016Z",
                        }
                    },
                ]
            }
        ],
    }

my_detail 应该只显示 my_detail 对象中的 first_name、last_name 和状态,而不是显示 _id、活动电子邮件、密码等所有内容,尝试不同的方式没有成功,请指导。

如何进行投影以显示有限的数据

$project : {
    "string":1,
    "records":1,
    "records.my_detail.first_name":1,
    "records.my_detail.last_name" :1,
    "records.my_detail.status" :1

}

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    你所做的是正确的,但是你有一个上层数组data[]。所以你也需要包含它

    db.collection.aggregate([
      {
        $project: {
          "data.records.my_detail.first_name": 1,
          "data.records.my_detail.last_name": 1,
          "data.records.my_detail.status": 1
        }
      }
    ])
    

    工作Mongo playground

    【讨论】:

    • 那么你不能在查询中使用mydetails吗?
    • 大声笑,我没有注意到你发表了评论,我以为帖子的所有者发表了评论。
    • 是的,我明白了。我们会看看他的评论,并会尝试根据他的要求给出解决方案
    • 你太棒了,你对每个问题都给出了快速的回复,非常感谢。
    • 哦,亲爱的,只需删除或评论这个 => "records":1
    【解决方案2】:

    如果不进行聚合,请使用它:

    db.collection.find({},
    {
    _id: 0,
    "data.records.my_detail.first_name": 1,
    "data.records.my_detail.last_name": 1,
    "data.records.my_detail.status": 1
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 2020-03-04
      • 1970-01-01
      • 2016-09-08
      相关资源
      最近更新 更多