Answers: <p>I was able to figure out how to update those records. This how the update_one command should be written:</p>
  <pre><code>collection_one.update_one(
                             {&quot;group_wallet_id&quot;: ObjectId(data[&quot;group_wallet_id&quot;])},
                                  {
                                      &quot;$set&quot;: {&quot;update_at&quot;: now},
                                      &quot;$inc&quot;: {&quot;total_donations&quot;: data[&quot;amount&quot;]},
                                      &quot;$push&quot;: {&quot;categories&quot;: {
                                          &quot;category_id&quot;: category_id,
                                          &quot;categories.$.donors_info&quot;: {
                                              &quot;user_id&quot;: ObjectId(user_id),
                                              &quot;total_balance&quot;: convert_to_decimal128(data[&quot;amount&quot;]),
                                              &quot;d_created_at&quot;: now,
                                              &quot;d_updated_at&quot;: now
                                          },
                                          &quot;c_created_at&quot;: now, &quot;c_updated_at&quot;: now,
                                          &quot;amount&quot;: convert_to_decimal128(data[&quot;amount&quot;]),
                                      }}}, session=session)
  </code></pre>
  <p>All the operations should be wrapped in a <strong>{}</strong> as seen above.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-06-20T23:30:36.127Z
Answers: 2022-06-20T23:30:36.127Z
Answers: George Udosen
Body: <p>I have this mongodb schema:</p>
  <pre><code>{
      &quot;bsonType&quot;: &quot;object&quot;,
      &quot;required&quot;: [
          &quot;group_wallet_id&quot;, &quot;categories&quot;,
          &quot;total_donations&quot;, &quot;created_at&quot;
      ],
      &quot;properties&quot;: {
          &quot;group_wallet_id&quot;: {
              &quot;bsonType&quot;: &quot;objectId&quot;,
              &quot;description&quot;: &quot;Id of the parent group&quot;
          },
          &quot;total_donations&quot;: {
              &quot;bsonType&quot;: &quot;decimal&quot;,
              &quot;description&quot;: &quot;Total donations&quot;
          },
          &quot;created_at&quot;: {
              &quot;bsonType&quot;: &quot;date&quot;,
              &quot;description&quot;: &quot;Wallet creation date&quot;
          },
          &quot;updated_at&quot;: {
              &quot;bsonType&quot;: &quot;date&quot;,
              &quot;description&quot;: &quot;Wallet update latest date&quot;
          },
          &quot;categories&quot;: {
              &quot;bsonType&quot;: &quot;array&quot;,
              &quot;items&quot;: {
                  &quot;bsonType&quot;: &quot;object&quot;,
                  &quot;required&quot;: [
                      &quot;category_id&quot;, &quot;donor_info&quot;,
                      &quot;c_created_at&quot;, &quot;c_updated_at&quot;
                  ],
                  &quot;properties&quot;: {
                      &quot;category_id&quot;: {
                          &quot;bsonType&quot;: &quot;objectId&quot;,
                          &quot;description&quot;: &quot;Category unique id&quot;
                      },
                      &quot;donors_info&quot;: {
                          &quot;bsonType&quot;: &quot;array&quot;,
                          &quot;items&quot;: {
                              &quot;bsonType&quot;: &quot;object&quot;,
                              &quot;required&quot;: [
                                  &quot;user_id&quot;, &quot;total_balance&quot;, &quot;d_created_date&quot;,
                                  &quot;d_updated_date&quot;
                              ],
                              &quot;properties&quot;: {
                                  &quot;user_id&quot;: {
                                      &quot;bsonType&quot;: &quot;objectId&quot;,
                                      &quot;description&quot;: &quot;User id&quot;
                                  },
                                  &quot;total_balance&quot;: {
                                      &quot;bsonType&quot;: &quot;decimal&quot;,
                                      &quot;description&quot;: &quot;Amount donated&quot;
                                  },
                                  &quot;d_created_at&quot;: {
                                      &quot;bsonType&quot;: &quot;date&quot;,
                                      &quot;description&quot;: &quot;The date the donation was made&quot;
                                  },
                                  &quot;d_updated_at&quot;: {
                                      &quot;bsonType&quot;: &quot;date&quot;,
                                      &quot;description&quot;: &quot;The latest update date&quot;
                                  }
                              }
  
                          }
                      },
                      &quot;amount&quot;: {
                          &quot;bsonType&quot;: &quot;decimal&quot;,
                          &quot;description&quot;: &quot;The total amount donated for per category&quot;
                      },
                      &quot;c_created_at&quot;: {
                          &quot;bsonType&quot;: &quot;date&quot;,
                          &quot;description&quot;: &quot;The date the donation was made&quot;
                      },
                      &quot;c_updated_at&quot;: {
                          &quot;bsonType&quot;: &quot;date&quot;,
                          &quot;description&quot;: &quot;The latest update date&quot;
                      }
                  }
              }
  
          }
      }
  }
  </code></pre>
  <p>I am trying to update the <strong>categories array</strong> and after that the <strong>donors_info</strong> nested array. I have this:</p>
  <pre><code>donor_3 = collection_one.update_one(
                                  {&quot;group_wallet_id&quot;: ObjectId(data[&quot;group_wallet_id&quot;])},
                                  {&quot;$set&quot;: {&quot;update_at&quot;: now}},
                                  {&quot;$inc&quot;: {&quot;total_donations&quot;: data[&quot;amount&quot;]}},
                                  {&quot;$push&quot;: {&quot;categories&quot;: {
                                      &quot;category_id&quot;: category_id,
                                      &quot;categories.$.donors_info&quot;: {
                                          &quot;user_id&quot;: ObjectId(user_id),
                                          &quot;total_balance&quot;: convert_to_decimal128(data[&quot;amount&quot;]),
                                          &quot;d_created_at&quot;: now,
                                          &quot;d_updated_at&quot;: now
                                      },
                                      &quot;c_created_at&quot;: now, &quot;c_updated_at&quot;: now,
                                      &quot;amount&quot;: convert_to_decimal128(data[&quot;amount&quot;]),
                                  }}}, session=session)
  </code></pre>
  <p>I have tried this and unfortunately I am unable to do this. I using the pymongo lib in my app to access the monogodb atlas instance.</p>
  
Tags: <mongodb>
Title: How to update nested arrays in mongodb database
UserDisplayName: George Udosen
ThreadText: # How to update nested arrays in mongodb database
  
  (Asked by: username_0 on 2022-06-20)
  
  I have this mongodb schema:
  
  ```
  {
      "bsonType": "object",
      "required": [
          "group_wallet_id", "categories",
          "total_donations", "created_at"
      ],
      "properties": {
          "group_wallet_id": {
              "bsonType": "objectId",
              "description": "Id of the parent group"
          },
          "total_donations": {
              "bsonType": "decimal",
              "description": "Total donations"
          },
          "created_at": {
              "bsonType": "date",
              "description": "Wallet creation date"
          },
          "updated_at": {
              "bsonType": "date",
              "description": "Wallet update latest date"
          },
          "categories": {
              "bsonType": "array",
              "items": {
                  "bsonType": "object",
                  "required": [
                      "category_id", "donor_info",
                      "c_created_at", "c_updated_at"
                  ],
                  "properties": {
                      "category_id": {
                          "bsonType": "objectId",
                          "description": "Category unique id"
                      },
                      "donors_info": {
                          "bsonType": "array",
                          "items": {
                              "bsonType": "object",
                              "required": [
                                  "user_id", "total_balance", "d_created_date",
                                  "d_updated_date"
                              ],
                              "properties": {
                                  "user_id": {
                                      "bsonType": "objectId",
                                      "description": "User id"
                                  },
                                  "total_balance": {
                                      "bsonType": "decimal",
                                      "description": "Amount donated"
                                  },
                                  "d_created_at": {
                                      "bsonType": "date",
                                      "description": "The date the donation was made"
                                  },
                                  "d_updated_at": {
                                      "bsonType": "date",
                                      "description": "The latest update date"
                                  }
                              }
  
                          }
                      },
                      "amount": {
                          "bsonType": "decimal",
                          "description": "The total amount donated for per category"
                      },
                      "c_created_at": {
                          "bsonType": "date",
                          "description": "The date the donation was made"
                      },
                      "c_updated_at": {
                          "bsonType": "date",
                          "description": "The latest update date"
                      }
                  }
              }
  
          }
      }
  }
  
  ```
  
  I am trying to update the **categories array** and after that the **donors\_info** nested array. I have this:
  
  ```
  donor_3 = collection_one.update_one(
                                  {"group_wallet_id": ObjectId(data["group_wallet_id"])},
                                  {"$set": {"update_at": now}},
                                  {"$inc": {"total_donations": data["amount"]}},
                                  {"$push": {"categories": {
                                      "category_id": category_id,
                                      "categories.$.donors_info": {
                                          "user_id": ObjectId(user_id),
                                          "total_balance": convert_to_decimal128(data["amount"]),
                                          "d_created_at": now,
                                          "d_updated_at": now
                                      },
                                      "c_created_at": now, "c_updated_at": now,
                                      "amount": convert_to_decimal128(data["amount"]),
                                  }}}, session=session)
  
  ```
  
  I have tried this and unfortunately I am unable to do this. I using the pymongo lib in my app to access the monogodb atlas instance.
  
  **Tags:** mongodb
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2022-06-20. Score: 0
  
  I was able to figure out how to update those records. This how the update\_one command should be written:
  
  ```
  collection_one.update_one(
                             {"group_wallet_id": ObjectId(data["group_wallet_id"])},
                                  {
                                      "$set": {"update_at": now},
                                      "$inc": {"total_donations": data["amount"]},
                                      "$push": {"categories": {
                                          "category_id": category_id,
                                          "categories.$.donors_info": {
                                              "user_id": ObjectId(user_id),
                                              "total_balance": convert_to_decimal128(data["amount"]),
                                              "d_created_at": now,
                                              "d_updated_at": now
                                          },
                                          "c_created_at": now, "c_updated_at": now,
                                          "amount": convert_to_decimal128(data["amount"]),
                                      }}}, session=session)
  
  ```
  
  All the operations should be wrapped in a **{}** as seen above.
  
  
  ---
  
  