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

4
  • So, what is the problem? Commented Oct 24, 2019 at 2:57
  • Take a look at the answer of this post an: 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. Commented Oct 24, 2019 at 5:25
  • You can explain the problem you are facing using a simple example (with sample data and code for that). Commented Oct 24, 2019 at 6:49
  • Possible duplicate of Selecting and updating a nested object by it's ObjectId in Mongoose.js Commented Oct 24, 2019 at 6:49

1 Answer 1

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",
                    }

                })
Sign up to request clarification or add additional context in comments.

1 Comment

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?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.