0

I have a document like this,

{
    "S" : {
        "500209" : {
            "total_income" : 38982,
            "interest_income" : 1714,
            "reported_eps" : 158.76,
            "year" : 201303
        }
    },
    "_id" : "pl"
}

I am trying to update this document like this,

{
    "S" : {
        "500209" : {
            "total_income" : 38982,
            "interest_income" : 1714,
            "reported_eps" : 158.76,
            "year" : 201303,
            "yield": 1001,  <== inserted a new attribute
        }
    },
    "_id" : "pl"
}

I have tried this,

db.my_collection.update({_id: 'pl'},{$set: {'S.500209.yield': 1}})

But I couldn't make it. And I searched in stack overflow and google but I couldn't find out.

I got so many answers but most of them keeping sub-docuemnts in array.

Pleas help me to solve my issue, and please tell me why most of them keeping subdocuments in array.

1 Answer 1

1

Number key might cause the problem. Update your field name.

db.my_collection.update({_id: 'pl'},{$set: {'S.a500209.yield': 1}})

EDIT

Upgrade mongo version. It works fine with 2.4.

Sign up to request clarification or add additional context in comments.

4 Comments

you are exactly correct. The problem is because of number. I have changed that number to a string and tried. It is working.
As I guess :) But the problem is definetely about numeric field name. S.500209 means that S is an array and I want 500209th element. Can you just add a prefix to your "500209" key and make it something like "a500209"? 'S.a500209.yield' would work, I think. If any solution without changing the field name is available, someone would enlighten us
I need a solution without changing field name :-) its hard for me to change field name.
I am using 2.2. Will upgrade it now ! Thanks a lot.

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.