【问题标题】:Update MongoDB object in object by id通过 id 更新对象中的 MongoDB 对象
【发布时间】:2019-08-29 17:37:28
【问题描述】:

型号:

const projectSchema = new Schema({
  name: {
    type: String,
    maxlength: 50
  },
  description: {
    type: String
  },
  projectLead: {
    type: String
  },
  start: {
    type: String
  },
  end: {
    type: String
  },
  projectType: {
    type: String
  },
  claimId: {
    type: String
  },
  organization: {
    type: String
  },
  seats: [{
    id: String,
    employee: String,
    role: String,
    start: String,
    end: String,
    workload: Number,
    skills: Array,
    approved: Boolean
  }]
}, {
  timestamps: true
})

模型响应示例:

{
    "project": {
        "_id": "5cab4b9bc9b29a7ba2363875",
        "name": "Project Title",
        "description": "Project description",
        "projectLead": "email@email",
        "start": "2018-06-01T09:45:00.000Z",
        "end": "2019-06-31T09:45:00.000Z",
        "claimId": "AIIIIII",
        "organization": "Company ACE",
        "seats": [
            {
                "skills": [
                    "Node.js",
                    "Vue.js"
                ],
                "_id": "5cab548e5cefe27ef82ca313",
                "start": "2018-06-01T09:45:00.000Z",
                "end": "2019-06-31T09:45:00.000Z",
                "role": "Developer",
                "approved": false,
                "workload": 20,
                "employee": ''
            }
        ],
        "createdAt": "2019-04-08T13:24:43.253Z",
        "updatedAt": "2019-04-08T14:02:54.257Z",
        "__v": 0
    }
}

控制器:

exports.updateSeatEmployee = async (req, res, next) => {
  try {
    const project = await Project.findOneAndUpdate({
      "seats._id": req.params.id
    }, {
      "seats.employee": req.body.employee
    }, {
      new: true
    })
    console.log("project", project);
    return res.json({
      project: project
    })
  } catch (error) {
    next(error)
  }
}

调试器:

Mongoose: users.createIndex({ email: 1 }, { unique: true, background: 真 }) { sub: '5cab48ebec24577ab3329fcd', iat: 1554729210 }

猫鼬:users.findOne({ _id: ObjectId("5cab48ebec24577ab3329fcd") }, { 投影:{} })

猫鼬:projects.findAndModify({ 'seats._id': ObjectId("5cab529bcafa027e30ee229c") }, [], { '$setOnInsert': { createdAt: new Date("Mon, 08 Apr 2019 14:45:56 GMT") }, '$set': { 'seats.$.employee':'email@email.com',更新时间:新 日期(“星期一,2019 年 4 月 8 日 14:45:56 GMT”)} },{ 新:真,更新:假, 删除:假,投影:{} })(节点:33302)弃用警告: 不推荐使用 collection.findAndModify。使用 findOneAndUpdate, 改为 findOneAndReplace 或 findOneAndDelete。

项目空

我想通过idseats 中搜索特定对象。 这个特定的座位应该更新。就我而言,我想更新 employee 字段。

如果我执行findOne({"seats._id": req.params.id}),我会返回项目,但findOneAndUpdate 返回null

【问题讨论】:

    标签: javascript mongodb mongoose mongodb-query


    【解决方案1】:

    您需要$ positional operator,因为seats 是一个数组,如果您只想更新一个字段,您应该使用$set 运算符

    const project = await Project.findOneAndUpdate({
      "seats._id": req.params.id
    }, {
      $set: { "seats.$.employee": req.body.employee }
    }
    

    【讨论】:

    • @mrks 你有没有尝试过这两种过滤条件,比如{ _id: ObjectId("5cab48ebec24577ab3329fcd"), "seats._id": req.params.id }
    猜你喜欢
    • 2017-08-17
    • 2020-07-19
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多