【问题标题】:how to query mongodb using findOne and exclude some nested fields in array如何使用 findOne 查询 mongodb 并排除数组中的一些嵌套字段
【发布时间】:2021-11-26 12:31:20
【问题描述】:

虽然我使用过 MySQL,但我是 MongoDB 新手。

我的用户集合具有以下形状的用户数据 -

我正在创建api in nodejs,它会获取给定用户的全部数据,但应该排除每种数据类型中的transactions 字段。 我的获取数据 api:localhost:5000/api/users/id/:id/data

请帮我写查询。我已经创建了 api 来获取所有用户、创建用户、更新用户,但我只坚持这个。我也尝试过使用聚合,但它会返回包括交易在内的整个数据。我的尝试-

getUserData = async (req, res) => {
    const { id } = req.params;
    try {
      const userData = await UserModel.findById(id).aggregate([
        { $project: { "data.v.transactions": 0 } }
      ]);
      if (!userData ) res.status(400).json({ success: false, message: `No data found` });
      res.status(200).send(userData);
    } catch (error) {
      console.error(error);
      res.status(400).send({ status: false, error });
    }
  }

我在我的 nodejs 应用程序中使用 mongoose 包。

用户集合中的一个用户对象 --

{
  "_id": "615d6a2afabf089b94a116e6",
  "email": "john@doe.com",
  "data": {
    "TYPE_1": [
      {
        "type": "type_1",
        "transactions": {
          "endDate": "2020-09-17T14:25:33.440Z",
          "startDate": "2019-04-11T11:39:57.153Z",
          "transaction": [
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            }
          ]
        },
        "profile": {
          "full_name": "John Doe"
        },
        "summary": {
          "note": "Lorem Ipsum"
        }
      },
      {
        "type": "type_1",
        "transactions": {
          "endDate": "2020-09-17T14:25:33.440Z",
          "startDate": "2019-04-11T11:39:57.153Z",
          "transaction": [
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            }
          ]
        },
        "profile": {
          "full_name": "John Doe"
        },
        "summary": {
          "note": "Lorem Ipsum"
        }
      },
      {
        "type": "type_1",
        "transactions": {
          "endDate": "2020-09-17T14:25:33.440Z",
          "startDate": "2019-04-11T11:39:57.153Z",
          "transaction": [
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            }
          ]
        },
        "profile": {
          "full_name": "John Doe"
        },
        "summary": {
          "note": "Lorem Ipsum"
        }
      }
    ],
    "TYPE_2": [
      {
        "type": "type_2",
        "transactions": {
          "endDate": "2020-09-17T14:25:33.440Z",
          "startDate": "2019-04-11T11:39:57.153Z",
          "transaction": [
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            }
          ]
        },
        "profile": {
          "full_name": "John Doe"
        },
        "summary": {
          "note": "Lorem Ipsum"
        }
      },
      {
        "type": "type_2",
        "transactions": {
          "endDate": "2020-09-17T14:25:33.440Z",
          "startDate": "2019-04-11T11:39:57.153Z",
          "transaction": [
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            }
          ]
        },
        "profile": {
          "full_name": "John Doe"
        },
        "summary": {
          "note": "Lorem Ipsum"
        }
      },
      {
        "type": "type_2",
        "transactions": {
          "endDate": "2020-09-17T14:25:33.440Z",
          "startDate": "2019-04-11T11:39:57.153Z",
          "transaction": [
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            },
            {
              "mode": "A",
              "type": "AB",
              "amount": "0"
            }
          ]
        },
        "profile": {
          "full_name": "John Doe"
        },
        "summary": {
          "note": "Lorem Ipsum"
        }
      }
    ]
  },
  "createdAt": "2021-10-06T09:19:39.378Z",
  "updatedAt": "2021-10-06T09:19:39.378Z",
  "__v": 0
}

【问题讨论】:

  • 你想要得到什么结果?
  • @TobiasS。没有交易字段的集合中的整个用户对象。

标签: node.js mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:

不妨试试这个:

const userData = await UserModel.find({}, { 
    "data.TYPE_1.transactions" : 0, 
    "data.TYPE_2.transactions" : 0  
})

【讨论】:

  • 但我需要从每个类型中排除事务,而不仅仅是 Type_1
  • 只有type_1和type_2吗?
  • @TobiasS。与其忽略您想要的字段,不如选择您想要想要的字段。这样可以确保如果您添加新字段,您不会无意中选择它们。
  • Tobias,可以有很多种类型,我只是嘲笑了一下。 @MikeEason 请通过排除不需要的字段或仅选择想要的字段来提供通用解决方案。
猜你喜欢
  • 1970-01-01
  • 2020-03-09
  • 2019-05-17
  • 1970-01-01
  • 2016-04-19
  • 2012-05-04
  • 1970-01-01
  • 2019-09-29
  • 2018-03-01
相关资源
最近更新 更多