【问题标题】:Mongoose select specific fields from arrayMongoose 从数组中选择特定字段
【发布时间】:2021-08-16 17:36:34
【问题描述】:

我正在尝试减少我的 API 数据大小以删除不需要的数据。我有这样的架构

const course = mongoose.Schema(
  {
    course_name: { type: String, require: true },
    disabled: { type: String, required: true, default: false },
    subject_ids: [
      {
        type: mongoose.Schema.ObjectId,
        ref: 'subject',
        require: true,
      },
    ],
  },
  { timestamps: true }
);

应用查找查询后,我有这样的数据

 {
        "disabled": "false",
        "subject_ids": [
            {
                "disabled": "false",
                "_id": "60b0bdd5cd7bd635ecf07cd5",
                "subject_name": "CSS",
                "createdAt": "2021-05-28T09:54:29.147Z",
                "updatedAt": "2021-05-28T09:54:29.147Z",
                "__v": 0
            },
            {
                "disabled": "false",
                "_id": "60b0bdd5cd7bd635ecf07cd7",
                "subject_name": "Jquery",
                "createdAt": "2021-05-28T09:54:29.147Z",
                "updatedAt": "2021-05-28T09:54:29.147Z",
                "__v": 0
            }
        ],
        "_id": "60b0e3f3012b2b272432e9f9",
        "course_name": "Data Science",
        "createdAt": "2021-05-28T12:37:07.103Z"
    }

API 我试过这样的事情。我已经从外部数组中删除了数据,但我不知道如何从内部删除它。我做了很多谷歌搜索,但我没有得到

router.get('/get-course/:status', async (req, res) => {
  try {
    const data = await COURSE.find({})
      .populate('subject_ids')
      .select({ updatedAt: 0, __v: 0 })
      .exec();

    res.json(data);
  } catch (error) {
    res.status(404).json({ err: 1, message: error.message, error });
  }
});

我希望数据应该是这样的

 {
        "disabled": "false",
        "subject_ids": [
            {
             
                "_id": "60b0bdd5cd7bd635ecf07cd5",
                "subject_name": "CSS",
               
            },
            {
               
                "_id": "60b0bdd5cd7bd635ecf07cd7",
                "subject_name": "Jquery",
              
            }
        ],
        "_id": "60b0e3f3012b2b272432e9f9",
        "course_name": "Data Science",
        "createdAt": "2021-05-28T12:37:07.103Z"
    }

如何从数组中获取特定数据

【问题讨论】:

  • 这能回答你的问题吗? Populating only specific fields in mongoose
  • 不。它不起作用
  • 不行,它不起作用 没有用,你得到了什么?
  • 我收到错误:-无效选择:选择只需要 1 个参数
  • 我认为您在选择中使用了第二个参数,您需要在填充中添加第二个参数,再次检查该问题。

标签: javascript node.js mongodb express


【解决方案1】:

试试这个:

populate('subject_ids','subject_name').exec()

【讨论】:

    【解决方案2】:

    试试这个

    router.get('/get-course/:status', async (req, res) => {
      try {
        const data = await COURSE.find({})
          .populate('subject_ids')
          .select({ 
            updatedAt: 0, 
            __v: 0,
            subject_ids.disabled: 0,
            subject_ids.createdAt: 0
            subject_ids.updatedAt: 0
            subject_ids.__v: 0
          })
          .exec();
        res.json(data);
      } catch (error) {
        res.status(404).json({ err: 1, message: error.message, error });
      }
    });
    

    【讨论】:

    • 我已经试过这个也喜欢 .select({ updatedAt: 0, __v: 0, 'subject_ids.disabled': 0 })
    猜你喜欢
    • 2016-04-13
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2016-12-10
    • 1970-01-01
    相关资源
    最近更新 更多