0

I was looking through StackOverflow, but most of the questions are related to updating of a single element of the object from the array.

In my case, I have a little different situation. I want to replace full object from an array with a new one.

The original collection looks like:

db.countries.find().pretty()
{
    "_id": "some ID goes here",
    "longitude": "some longitude",
    "latitude": "some latitude",
    "name": "name of some country",
    "millionaires": [
        {"millionaire_id": "ID of the first millionaire",
         "name": "name of the first millionaire",
         "money": 12000000,
        },
        {"millionaire_id": "ID of the second millionaire",
         "name": "name of the second millionaire",
         "money": 15000000,
        },
    ],
}

Then for some reason, I want to replace some data for the first millionaire(amount of money changed, etc.). NOTE: I don't know which field changed.

I'm trying to do something like this:

db.countries.update({"_id": "country ID",
                     "millionaires.millionaire_id": "ID of the first millionaire"
                    },
                    {$set: {
                             "millionaire_id": "the same millionaire ID",
                             "value": "should be another value", ...
                           }
                    })

But that works unexpectedly:

db.countries.find().pretty();
{
"_id": "some ID goes here",
"longitude": "some longitude",
"latitude": "some latitude",
"name": "name of some country",
"millionaires": [
    {"millionaire_id": "ID of the first millionaire",
     "name": "name of the first millionaire",
     "money": 12000000,
    },
    {"millionaire_id": "ID of the second millionaire",
     "name": "name of the second millionaire",
     "money": 15000000,
    },
],
{
 "millionaire_id": "the same millionaire ID",
 "value": "should be another value", ...
}
}

So, it added a new object out of my list of millionaires.

NOTE: objects for updates go programmatically that's why it's not a good idea to try something like this:

new_object = {"millionaires. millionaire_id": "ID", "millionaires.name": "name", ...}

I have raw objects and want to update existing elements using this objects.

Any ideas?

smart
  • 1,975
  • 5
  • 26
  • 46
  • @rrrr, well, as I mentioned above, this is just an update of one field of a document. I need an update of the full document. Do you feel the difference? – smart Aug 16 '17 at 12:36
  • I think the duplicate is actually wrong however there is a duplicate that is more fitting in https://stackoverflow.com/questions/29012328/how-to-update-a-subdocument-in-mongodb. The key is the setting of `$`. – rrrr-o Aug 17 '17 at 14:15
  • I'm not going to override the decision of a gold tag badge holder, but I did *add* the duplicate @rrrr suggested to the list. If you still don't think this is a duplicate, smart, then you need to follow the advice underneath the question: [edit] your question, explain *why* it is not a duplicate (be detailed and specific), and that will nominate it for re-opening. – Cody Gray - on strike Aug 17 '17 at 14:25

0 Answers0