0

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.

1 Answers1

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.