【问题标题】:Mongoose Populate Multiple FieldsMongoose 填充多个字段
【发布时间】:2019-11-16 02:29:07
【问题描述】:

我正在尝试像使用关系数据库一样使用 Mongoose。我有一个需要引用客户端、用户和笔记架构的任务架构。

我找到了许多用另一种模式填充一个模式的示例,但是当我需要填充多个模式时却没有。我尝试了以下链接: https://mongoosejs.com/docs/populate.html http://ronaldroe.com/populating-multiple-fields-and-levels-with-mongoose/

findAll: function (req, res) {
    Task
      .find(req.query)
      .sort({ date: -1 })
      // populate associated user
      .populate("user", "_id firstName lastName")
      // populate associated client
      .populate("client", "_id name")
      // all notes for the task and when they were created
      .populate("note", "_id content created_at")
      .then(dbModel => {
        res.status(200).json({
          tasks: dbModel.map(model => {
            return {
              _id: model._id,
              user: model.user,
              client: model.client,
              assignDate: model.assignDate,
              assignedStatus: model.assignedStatus,
              completionStatus: model.completionStatus,
              description: model.description,
              note: model.note,
            };
          })
        })
      })
      .catch(err => res.status(422).json(err));
  }

当我对此数据执行 GET 请求时,我得到以下信息:

"tasks": [
        {
            "_id": "5d6980d8459a8b9f0e133d04",
            "user": [
                {
                    "_id": "5d6afd8a101f355244adfd9a",
                    "firstName": "Dexter",
                    "lastName": "Morgan"
                }
            ],
            "assignDate": "2018-12-09T00:00:00.000Z",
            "assignedStatus": "true",
            "completionStatus": "in-progress",
            "description": "This client needs to be contacted for future sales"
        }
]

我还想获取客户端 ID 和名称以及与此任务相关的所有注释。

【问题讨论】:

    标签: javascript mongodb api mongoose


    【解决方案1】:

    试试下面的代码:

    findAll: function (req, res) {
        Task
          .find(req.query)
          .sort({ date: -1 })
          // populate associated user
          .populate({path : 'user' , select: '_id firstName lastName'}) 
          // populate associated client
          .populate({path : 'client' , select: '_id name'})
          // all notes for the task and when they were created
          .populate({path : 'note' , select : '_id content created_at'})
          .then(dbModel => {
            res.status(200).json({
              tasks: dbModel.map(model => {
                return {
                  _id: model._id,
                  user: model.user,
                  client: model.client,
                  assignDate: model.assignDate,
                  assignedStatus: model.assignedStatus,
                  completionStatus: model.completionStatus,
                  description: model.description,
                  note: model.note,
                };
              })
            })
          })
          .catch(err => res.status(422).json(err));
      }
    

    【讨论】:

      猜你喜欢
      • 2021-11-03
      • 2012-01-22
      • 2017-05-21
      • 2015-06-09
      • 1970-01-01
      • 2021-09-27
      • 2013-07-06
      • 2021-09-06
      • 1970-01-01
      相关资源
      最近更新 更多