Update specific value in array mongoose

Germain Perdigal

I'm trying to update my hairdresser schema, and more precisely one object of my services array.

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
});

I tried to use $set and $ notation in order to target the desired object but Mongo keeps telling me that it's impossible to create a property.

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

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

Here is a JSON formatted example of my worker schema :

{ _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 } ]
}

I need to upd Any idea how to make it work ?

SuleymanSah

You can use the filtered positional operator $ to update nested arrays.

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);
});

Let's say we have this document:

{
    "_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
}

If we want to update the stop with _id 5e5f8a2bf9f62d7558ad3bec for this document with _id: "5e5f8a2bf9f62d7558ad3be7", we send a PUT request http://.../workers/5e5f8a2bf9f62d7558ad3be7/5e5f8a2bf9f62d7558ad3bec

The result will be:

{
    "_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
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Update specific value in structure

分類Dev

How to update an array with his index number in Mongoose

分類Dev

Dynamically update key in array of objects + mongoose

分類Dev

Update specific value of object in localStorage

分類Dev

How to sort mongoose populated array by a value?

分類Dev

Python - search for a specific value in an array

分類Dev

how does one update numbers inside mongoose model nested array

分類Dev

React useState to update a specific item in its array

分類Dev

Remove or Find an object with Mongoose when it is in an array by the object property value

分類Dev

Update null and specific value using first value of each column and specific value respectively

分類Dev

Count number of items in an array with a specific property value

分類Dev

How to get specific array value in php

分類Dev

Update array of string with value from config

分類Dev

Update object value in array React hooks way

分類Dev

Mongoose bulk update operation

分類Dev

hashing password on update with mongoose

分類Dev

Mongoose update after save

分類Dev

Get all documents of a type in mongoose but only with 1 specific item of each documents array

分類Dev

Mongoose - Save array of strings

分類Dev

Mongoose populate nested array

分類Dev

Mongoose not saving array of objects

分類Dev

Array in User Schema - mongoose

分類Dev

update a document in mongodb using mongoose

分類Dev

how it works with mongoose multiple update?

分類Dev

Update a document with mongoose (update property of document property)

分類Dev

PHP Multidimensional Array Searching (Find key by specific value)

分類Dev

JavaScript/VueJS: Check if an Array contains an object with an element that has specific value

分類Dev

How to find if specific key with value exist in array jQuery?

分類Dev

Getting multiple specific key value pairs if exist in associate array

Related 関連記事

  1. 1

    Update specific value in structure

  2. 2

    How to update an array with his index number in Mongoose

  3. 3

    Dynamically update key in array of objects + mongoose

  4. 4

    Update specific value of object in localStorage

  5. 5

    How to sort mongoose populated array by a value?

  6. 6

    Python - search for a specific value in an array

  7. 7

    how does one update numbers inside mongoose model nested array

  8. 8

    React useState to update a specific item in its array

  9. 9

    Remove or Find an object with Mongoose when it is in an array by the object property value

  10. 10

    Update null and specific value using first value of each column and specific value respectively

  11. 11

    Count number of items in an array with a specific property value

  12. 12

    How to get specific array value in php

  13. 13

    Update array of string with value from config

  14. 14

    Update object value in array React hooks way

  15. 15

    Mongoose bulk update operation

  16. 16

    hashing password on update with mongoose

  17. 17

    Mongoose update after save

  18. 18

    Get all documents of a type in mongoose but only with 1 specific item of each documents array

  19. 19

    Mongoose - Save array of strings

  20. 20

    Mongoose populate nested array

  21. 21

    Mongoose not saving array of objects

  22. 22

    Array in User Schema - mongoose

  23. 23

    update a document in mongodb using mongoose

  24. 24

    how it works with mongoose multiple update?

  25. 25

    Update a document with mongoose (update property of document property)

  26. 26

    PHP Multidimensional Array Searching (Find key by specific value)

  27. 27

    JavaScript/VueJS: Check if an Array contains an object with an element that has specific value

  28. 28

    How to find if specific key with value exist in array jQuery?

  29. 29

    Getting multiple specific key value pairs if exist in associate array

ホットタグ

アーカイブ