0

I have following structure of my mongodb I want to update medicine the how is I'm new in mongodb.

I can able to add and delete in medicine but how to update medicine.

    "visitInfo": {
        "conditions": [
            "Musculoskeletal Pain"
        ],
        "visitDate": "2019-10-07T13:59:00.000Z",
        "cpInfo": "Dr. Rustom",

            "isDummyUser": false,
            "loginType": "client",
            "createdAt": "2019-09-20T12:37:47.542Z",
            "updatedAt": "2019-10-21T06:17:22.717Z",
            "id": "5d84c81b9451de4de00cfaca"
        }
    },
    "medicine": [
        {
            "doseTime": {
                "doseTimes": [
                    {
                        "_id": "5dad86a0bfc4c07a1ee2334f",
                        "medicineTakenTime": "",
                        "time": "9:00 AM",
                        "quantity": "Take 1"
                    }
                ],
                "doseTypes": "daily (no abbreviation)"
            },
            "schedule": {
                "duration": "365 Days",
                "startDate": "2019-10-21T00:00:00.000Z"
            },
            "_id": "5dad86a0bfc4c07a1ee2334e",
            "medicineName": "TRIBENZOR (Oral Pill)",
            "medicineIcon": "Circle",
            "strength": " 5-12.5-20 mg Tab",
            "instruction": "After eating"
        },
        {
            "doseTime": {
                "doseTimes": [
                    {
                        "_id": "5dad86a0bfc4c07a1ee2334f",
                        "medicineTakenTime": "",
                        "time": "9:00 AM",
                        "quantity": "Take 1"
                    }
                ],
                "doseTypes": "daily (no abbreviation)"
            },
            "schedule": {
                "duration": "365 Days",
                "startDate": "2019-10-21T00:00:00.000Z"
            },
            "_id": "5dad86a0bfc4c07a1ee2334e",
            "medicineName": "TRIBENZOR (Oral Pill)",
            "medicineIcon": "Circle",
            "strength": " 5-12.5-20 mg Tab",
            "instruction": "After eating"
        },
        {
            "doseTime": {
                "doseTimes": [
                    {
                        "_id": "5dad86a0bfc4c07a1ee2334f",
                        "medicineTakenTime": "",
                        "time": "9:00 AM",
                        "quantity": "Take 1"
                    }
                ],
                "doseTypes": "daily (no abbreviation)"
            },
            "schedule": {
                "duration": "365 Days",
                "startDate": "2019-10-21T00:00:00.000Z"
            },
            "_id": "5dad86a0bfc4c07a1ee2334e",
            "medicineName": "TRIBENZOR (Oral Pill)",
            "medicineIcon": "Circle",
            "strength": " 5-12.5-20 mg Tab",
            "instruction": "After eating"
        }
    ]

Code:

db.getCollection("visits").update({ _id:ObjectId('5d9b1d859a1a4835a4c438ee'),"medicine._id": ObjectId("5d9b1d859a1a4835a4c438f1") },
{ $set: { 
    "medicine.$.medicineName": "TRIBENZOR (Oral Pill) updated dfgdfg 1", 
    "medicine.$.insttruction": "mid night 2", 
    "medicine.$.medicineIcon": "midhyperbola 3",
    "medicine.$.strength": "midhyperbola 4",
    "medicine.$.schedule.duration": "4 Days",

    } 

})

I want to update the medicine in all field

halfer
  • 19,824
  • 17
  • 99
  • 186
  • So, what is the problem? – prasad_ Oct 24 '19 at 02:57
  • Take a look at the answer of this post an: [MongoDB - Update an object in nested Array](https://stackoverflow.com/questions/34431435/mongodb-update-an-object-in-nested-array). Also, do a search (Google, etc.) with a string _"mongodb update array of objects"_, and you will see similar posts and their answers; I am sure you will find clarification to what you are looking for. – prasad_ Oct 24 '19 at 05:25
  • You can explain the problem you are facing using a simple example (with sample data and code for that). – prasad_ Oct 24 '19 at 06:49
  • Possible duplicate of [Selecting and updating a nested object by it's ObjectId in Mongoose.js](https://stackoverflow.com/questions/48594481/selecting-and-updating-a-nested-object-by-its-objectid-in-mongoose-js) – Vikash_Singh Oct 24 '19 at 06:49

1 Answers1

0
db.getCollection("visits").update({
                "_id": req.params.id,
                "medicine": {
                    "$elemMatch": {
                        "_id": req.params.medicineId
                    }
                }
            }, {
                    $set: {

                        "medicine.$.medicineName": "TRIBENZOR (Oral Pill) updated dfgdfg 1", 
                       "medicine.$.insttruction": "mid night 2", 
                       "medicine.$.medicineIcon": "midhyperbola 3",
                        "medicine.$.strength": "midhyperbola 4",
                        "medicine.$.schedule.duration": "4 Days",
                    }

                })
vicky
  • 415
  • 2
  • 10
  • You don't need to use `$elemMatch` to query on a single field (of an embedded document in an array). See this post, [MongoDB: what is the difference between $elemMatch and $and to find objects inside array?](https://stackoverflow.com/questions/58384199/mongodb-what-is-the-difference-between-elemmatch-and-and-to-find-objects-insi/58387242#58387242). – prasad_ Oct 24 '19 at 06:24