【问题标题】:MongoDB query fetch with without unwanted data using NestJS使用 NestJS 的 MongoDB 查询获取没有不需要的数据
【发布时间】:2021-03-04 13:10:47
【问题描述】:
{
    "_id": {
        "$oid": "5f244a0c9b20935b3bdc2cd9"
    },
    "user": {
        "rid": "5f12ba968ca0084ee7f7ea9b",
        "name": "",
        "phone": “0123456789”
    },
    "clazz": “Test”,
    "enrolment_history": [{
        "_id": {
            "$oid": "5f245e2f1906715c9e52d675"
        },
        "start_from": {
            "$date": "2020-06-30T18:30:00.000Z"
        },
        "expires_in": {
            "$date": "2020-07-31T18:30:00.000Z"
        },
        "enrollment_date": {
            "$date": "2020-06-30T18:30:00.000Z"
        },
        "payment_method": "Free",
        "status": "SUCCESS",
        "clazz_transaction": "5f3e74f8d26e962003103c11",
        "month": "2020-6"
    }, {
        "_id": {
            "$oid": "5f3e790c1e3c741f9eb6e53b"
        },
        "start_from": {
            "$date": "2020-07-31T18:30:00.000Z"
        },
        "expires_in": {
            "$date": "2020-08-31T18:30:00.000Z"
        },
        "enrollment_date": {
            "$date": "2020-07-31T18:30:00.000Z"
        },
        "payment_method": "Free",
        "status": "SUCCESS",
        "clazz_transaction": "5f3e790c1e3c741f9eb6e53a",
        "month": "2020-7"
    }, {
        "_id": {
            "$oid": "5f5763bfc2464554c15a2b28"
        },
        "start_from": {
            "$date": "2020-08-31T18:30:00.000Z"
        },
        "expires_in": {
            "$date": "2020-09-30T18:30:00.000Z"
        },
        "enrollment_date": {
            "$date": "2020-09-08T10:58:07.333Z"
        },
        "payment_method": "Free",
        "status": "SUCCESS",
        "clazz_transaction": "5f5763bfc2464554c15a2b27",
        "month": "2020-8"
    }
}

我有那种文档列表,所以我需要使用下面的过滤器来获取这个文档

{"phone":"0123456789","enrolment_history.month":"2020-6"}

输出 => (删除 enrolment_history 数组中的其他数据)

{
    "_id": {
        "$oid": "5f244a0c9b20935b3bdc2cd9"
    },
    "user": {
        "rid": "5f12ba968ca0084ee7f7ea9b",
        "name": "",
        "phone": “0123456789”
    },
    "clazz": “Test”,
    "enrolment_history": [{
        "_id": {
            "$oid": "5f245e2f1906715c9e52d675"
        },
        "start_from": {
            "$date": "2020-06-30T18:30:00.000Z"
        },
        "expires_in": {
            "$date": "2020-07-31T18:30:00.000Z"
        },
        "enrollment_date": {
            "$date": "2020-06-30T18:30:00.000Z"
        },
        "payment_method": "Free",
        "status": "SUCCESS",
        "clazz_transaction": "5f3e74f8d26e962003103c11",
        "month": "2020-6"
    }
}

【问题讨论】:

    标签: mongodb mongoose nestjs


    【解决方案1】:

    你可以使用aggregate()方法,

    • $match 根据您的条件过滤文档
    • $set 使用 $filter 进行过滤
    ModelName.aggregate([
      {
        $match: {
          "user.phone": "0123456789",
          "enrolment_history.month": "2020-6"
        }
      },
      {
        $set: {
          "enrolment_history": {
            $filter: {
              input: "$enrolment_history",
              cond: { $eq: ["$$this.month", "2020-6"] }
            }
          }
        }
      }
    ])
    

    Playground

    【讨论】:

      猜你喜欢
      • 2020-06-19
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多