0

First I will give you a simple output:

{
    "_id" : ObjectId("6016fcca3a406de622cfdc8f"),
    "pfname" : "charbel",
    "plname" : "alam",
    "address" : "Baskinta",
    "gender" : "Male",
    "phone" : "",
    "age" : "22",
    "addmissions" : [
            {
                    "sickness" : "diabete",
                    "arrival_date" : "13-1-2021",
                    "payments" : [ ]
            }
    ],
    "consultations" : [ ]

}

I am new to MongoDB and I want to add a document inside the array "payments" which is inside the "addmissions" array

I have been trying this command:

db.patients.update( {"pfname":"charbel"}, {$set:{"addmissions": {"sickness":"diabete","arrival_date":"13-1-2021", {$push:{"payments":{"medicaments":[],"surgeries":[],"total_price":"100000","amount_paid":"60000","amount_left":"40000"}}} }} });

But I am getting the following error: uncaught exception: SyntaxError: expected property name, got '{' :

I also tried something like this:

 db.patients.update({"pfname"
    :"charbel"},{$push:{addmissions.payments:{"medicaments":[],"surgeries":[],"total_price":"100000","amount_paid":"60000","amount_left":"40000"}}})

It is giving me this error: uncaught exception: SyntaxError: missing : after property id :

I also want to add a record to the medicaments array I am trying this query:

db.patients.update({
"pfname":"charbel",
    "addmissions.sickness":"diabete",
        "addmissions.arrival_date":"13-1-2021",
        "addmissions.payments.total_price":"100000",
        "addmissions.payments.amount_paid":"60000",
        "addmissions.payments.amount_left":"40000"
        },{$push:{"addmissions.$.payments.medicaments":{"medname":"panadol","medprice":"30000"}}})



But it is giving me this error:
                "errms

g" : "Cannot create field 'medicaments' in element {payments: [ { medicaments: [], surgeries: [], total_price: "100000", amount_paid: "60000", amount_left: "40000" } ]}"

2
  • MongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document. Commented Jan 31, 2021 at 20:10
  • I edited my question, can you please check it out?? Commented Jan 31, 2021 at 20:31

1 Answer 1

1

Try this.

db.collection.update(
   <query>,
   <update>
);

query: The selection criteria for the update. If you intend to update a single document, made sure that these criteria are unique.

update: The modifications to apply. Here we uses the dot notation to access the elements of admissions array and to access the comments field.

db.collection.update({
  "pfname": "charbel",
  "addmissions.sickness": "diabete",
  "addmissions.arrival_date": "13-1-2021"
},
{
  $push: {
    "addmissions.$.payments": {
      "medicaments": [],
      "surgeries": [],
      "total_price": "100000",
      "amount_paid": "60000",
      "amount_left": "40000"
    }
  }
})
Sign up to request clarification or add additional context in comments.

4 Comments

Can you please explain to me why did you put the $ sign between addmissions and payments? Cause I am trying to add a document in the medicaments array
The $ operator projects the first matching array element from each document in a collection based on some condition from the query statement. Basically $push into the payments field of the first document in the addmissions array that match my query
Okay , thanks a lot! Can you please help with one more thing? How can I add a record to the medicaments array?
Read this: docs.mongodb.com/manual/reference/operator/update/…, if still having problem ask another question.

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.