mongo db add / update array in document with mongo php - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Friday, June 11, 2021

mongo db add / update array in document with mongo php

I hope someone here can help. I am having trouble targeting a single object in a complex array for update. I provide all fields for the object changed or not but I cannot get an update to work I have used $push to create the multiple objects which seems to work fine, but if I try to update one of them with $set I end up just one object in the array with all the values in it. It may well be that my structure is incorrect and I can change if necessary

The relevant part of the document is

{
        "_id" : ObjectId("604b699228b6a51f7d6a4292"),
        "settings" : [
                {
                        "default" : {
                                "filters" : {
                                        "type_id" : "getwin_over15",
                                        "type_name" : "Over 1.5",
                                        "sport_id" : "1",
                                        "sport_name" : "Soccer",
                                        "league_id" : 0,
                                        "league_name" : "All Leagues",
                                        "teama_id" : 0,
                                        "teama_name" : "Team A",
                                        "teamb_id" : 0,
                                        "teamb_name" : "Team B",
                                        "threshold_id" : 75,
                                        "threshold_name" : "75%",
                                        "inby" : "by",
                                        "inbypd" : 5,
                                        "mode" : "Profiles",
                                        "histmatches" : "2"
                                },
                                "trading" : {
                                        "price" : "222",
                                        "stake" : "3355",
                                        "matched" : "0",
                                        "back_lay" : "back",
                                        "allowed_time_min" : "5.00",
                                        "allowed_time_max" : "90.00",
                                        "inplay" : {
                                                "attacks" : {
                                                        "homeaway" : "home",
                                                        "min" : "6123343",
                                                        "max" : "5333",
                                                        "dispname" : "Attacks"
                                                },
                                                "dattacks" : {
                                                        "homeaway" : "away",
                                                        "min" : "1",
                                                        "max" : "2",
                                                        "dispname" : "Dangerous Attacks"
                                                },
                                                "yellowcards" : {
                                                        "homeaway" : "both",
                                                        "min" : "1",
                                                        "max" : "6",
                                                        "dispname" : "Yellow Cards"
                                                }
                                        }
                                }
                        }
                },
                {
                        "new_prof" : {
                                "filters" : {
                                        "type_id" : "getwin_over15",
                                        "type_name" : "Over 1.5",
                                        "sport_id" : "1",
                                        "sport_name" : "Soccer",
                                        "league_id" : 0,
                                        "league_name" : "All Leagues",
                                        "teama_id" : 0,
                                        "teama_name" : "Team A",
                                        "teamb_id" : 0,
                                        "teamb_name" : "Team B",
                                        "threshold_id" : 75,
                                        "threshold_name" : "75%",
                                        "inby" : "by",
                                        "inbypd" : 5,
                                        "mode" : "Profiles",
                                        "histmatches" : "2"
                                },
                                "trading" : {
                                        "price" : "222",
                                        "stake" : "3355",
                                        "matched" : "0",
                                        "back_lay" : "back",
                                        "allowed_time_min" : "5.00",
                                        "allowed_time_max" : "90.00",
                                        "inplay" : {
                                                "attacks" : {
                                                        "homeaway" : "home",
                                                        "min" : "6123343",
                                                        "max" : "5333",
                                                        "dispname" : "Attacks"
                                                },
                                                "dattacks" : {
                                                        "homeaway" : "away",
                                                        "min" : "1",
                                                        "max" : "2",
                                                        "dispname" : "Dangerous Attacks"
                                                },
                                                "yellowcards" : {
                                                        "homeaway" : "both",
                                                        "min" : "1",
                                                        "max" : "6",
                                                        "dispname" : Yellow Cards"
                                                }
                                        }
                                }
                        }
                }
        ]
}

I need to target 'default' or 'new_prof' or others and will be providing all the subfields for the update, to overwrite what is there but only for the selected object

I have not managed to construct a $set that will do what I need.



source https://stackoverflow.com/questions/67926900/mongo-db-add-update-array-in-document-with-mongo-php

No comments:

Post a Comment