John Prawyn
John Prawyn

Reputation: 1693

mongo db update subdocument - subdocument not in array

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.

Upvotes: 0

Views: 623

Answers (1)

Mustafa Gen&#231;
Mustafa Gen&#231;

Reputation: 2579

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.

Upvotes: 1

Related Questions