更新数组猫鼬中的特定值

时间:2020-03-04 08:21:43

标签: mongodb mongoose

我正在尝试更新我的理发师架构,更确切地说是我的服务数组中的一个对象。

const hairdresserSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  email: { type: String, required: true },
  password: { type: String, required: true },
  label: { type: String, required: true },
  description: { type: String, required: true },
  stripe: { type: String, required: false },
  phone: { type: String, required: true },
  address: { type: String, required: true },
  services: [
    {
      _id: { type: mongoose.Schema.Types.ObjectId },
      label: { type: String },
      service: [{ type: mongoose.Schema.Types.ObjectId, ref: "category" }],
      price: { type: Number },
      supplement: { type: Number },
      duration: { type: Number }
    }
  ]
});

const workerSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  label: { type: String, required: true },
  entity: { type: mongoose.Schema.Types.ObjectId, required: true, ref: "hairdresser" },
  description: { type: String, required: true },
  availability: [
    {
      id: { type: mongoose.Schema.Types.ObjectId, required: false },
      date: { type: Date, required: false },
      stops: [
        {
          stop: { type: String },
          booked: { type: Boolean }
        }
      ]
    }
  ],
  required: false
});

我试图使用$ set和$表示法来定位所需的对象,但是Mongo一直告诉我不可能创建属性。

 worker.update(
        {
            'availability.stops._id': req.body.basicID
        },

        {
            $set: {
                'availability.stops.$.booked': true,
            }
        }
    )

这是我的工作模式的JSON格式示例:

{ _id: 5e4d6adbb12e8b5ccbb87d94,
  label: 'Germain',
  entity: 5e3d39fb0b40f96f98d33c1a,
  description: 'Je prendrai soin de vos cheveux',
  availability:
   [ { stops: [{ _id: 5e4d6adbsdfds2e8b5ccbb87d75, stop: "10:00", booked: false }, { _id: 5e4d6adbsdfds2sdfd8b5ccbb87d75, stop: "10:15", booked: false }],
       _id: 5e5edfd15605520adb7af977,
       date: 2020-03-03T23:00:00.000Z },
     { stops: [{ _id: 5e4d6adbb12e8b5ccbb87d92, stop: "08:00", booked: false }, { _id: 5e4d6adbb12e8b5ccbb87d75, stop: "08:15", booked: false }],
       _id: 5e5edfe05605520adb7af988,
       date: 2020-03-03T23:00:00.000Z } ],
  __v: 0 } ]
}

我需要更新 知道如何使它起作用吗?

1 个答案:

答案 0 :(得分:0)

您可以使用filtered positional operator $更新嵌套数组。

router.put("/workers/:workerId/:stopId", async (req, res) => {
  const result = await Worker.findByIdAndUpdate(
    req.params.workerId,
    {
      $set: {
        "availability.0.stops.$[stopId].booked": true
      }
    },
    {
      arrayFilters: [{ "stopId._id": req.params.stopId }],
      new: true
    }
  );
  res.send(result);
});

假设我们有此文档:

{
    "_id": "5e5f8a2bf9f62d7558ad3be7",
    "label": "Germain",
    "entity": "5e3d39fb0b40f96f98d33c1a",
    "description": "Je prendrai soin de vos cheveux",
    "availability": [
        {
            "_id": "5e5f8a2bf9f62d7558ad3beb",
            "stops": [
                {
                    "_id": "5e5f8a2bf9f62d7558ad3bed",
                    "stop": "10:00",
                    "booked": false
                },
                {
                    "_id": "5e5f8a2bf9f62d7558ad3bec",
                    "stop": "10:15",
                    "booked": false
                }
            ],
            "date": "2020-03-03T23:00:00.000Z"
        },
        {
            "_id": "5e5f8a2bf9f62d7558ad3be8",
            "stops": [
                {
                    "_id": "5e5f8a2bf9f62d7558ad3bea",
                    "stop": "08:00",
                    "booked": false
                },
                {
                    "_id": "5e5f8a2bf9f62d7558ad3be9",
                    "stop": "08:15",
                    "booked": false
                }
            ],
            "date": "2020-03-03T23:00:00.000Z"
        }
    ],
    "__v": 0
}

如果要使用5e5f8a2bf9f62d7558ad3bec将此文档的_id _id: "5e5f8a2bf9f62d7558ad3be7"更新为停靠点,则发送PUT请求http://.../workers/5e5f8a2bf9f62d7558ad3be7/5e5f8a2bf9f62d7558ad3bec

结果将是:

{
    "_id": "5e5f8a2bf9f62d7558ad3be7",
    "label": "Germain",
    "entity": "5e3d39fb0b40f96f98d33c1a",
    "description": "Je prendrai soin de vos cheveux",
    "availability": [
        {
            "stops": [
                {
                    "_id": "5e5f8a2bf9f62d7558ad3bed",
                    "stop": "10:00",
                    "booked": false
                },
                {
                    "_id": "5e5f8a2bf9f62d7558ad3bec",
                    "stop": "10:15",
                    "booked": true   => UPDATED
                }
            ],
            "_id": "5e5f8a2bf9f62d7558ad3beb",
            "date": "2020-03-03T23:00:00.000Z"
        },
        {
            "stops": [
                {
                    "_id": "5e5f8a2bf9f62d7558ad3bea",
                    "stop": "08:00",
                    "booked": false
                },
                {
                    "_id": "5e5f8a2bf9f62d7558ad3be9",
                    "stop": "08:15",
                    "booked": false
                }
            ],
            "_id": "5e5f8a2bf9f62d7558ad3be8",
            "date": "2020-03-03T23:00:00.000Z"
        }
    ],
    "__v": 0
}