2

Using mongodb with pymongo, I have the following document:

{
    "_id" : ObjectId("5515d697453d9a1975123e0b"),
    "Study_Attributes" : [ 
        {
            "value" : "183",
            "tag" : "height",
            "unit" : "cm"
        }, 
        {
            "value" : "92",
            "tag" : "weight",
            "unit" : "kg"
        }
    ],
    "Center_Project_Name" : "prj001",
    "Study_Abstract" : "THIS IS THE ABSTRACT",
    "Study_Description" : "---",
    "Study_Title" : "Study of Wheat",
    "Center_Name" : "cc001",
    "samples" : [ 
        {
            "Scientific_Name" : "aribido",
            "Anonymized_Name" : "XXX001",
            "Description" : "cress",
            "Characteristics" : [ 
                {
                    "value" : "",
                    "id" : ObjectId("5515d6f5453d9a1975123e0c"),
                    "tag" : "",
                    "unit" : ""
                }
            ],
            "Sample_Name" : "XXX001",
            "Common_Name" : "mustard cress",
            "Term_Accession_Number" : "19879",
            "Individual_Name" : "xx1",
            "Taxon_ID" : "19879",
            "_id" : ObjectId("5515d6f5453d9a1975123e0d"),
            "Term_Source_REF" : "TODO:ONTOTLOGY_ID",
            "Protocol_REF" : "TODO:PROTOCOL_STRING",
            "Source_Name" : "cthulu"
        }
    ]
}

and I want to update the sample subdocument (which is an array element since there may be multiple samples). I have the following code, but its not working...

EnaCollections.update(
            {"_id": o.ObjectId(study_id), "samples._id": o.ObjectId(sample_id)},
            {'$set:': {
                "samples.$.Source_Name": sample['Source_Name'],
                "samples.$.Characteristics": spec_attr,
                "samples.$.Term_Source_REF": "TODO:ONTOLOGY",
                "samples.$.Protocol_REF": "TODO:PROTOCOL_STRING",
                "samples.$.Sample_Name": sample['Anonymized_Name'],
                "samples.$.Individual_Name": sample['Individual_Name'],
                "samples.$.Description": sample['Description'],
                "samples.$.Taxon_ID": sample['Taxon_ID'],
                "samples.$.Scientific_Name": sample['Scientific_Name'],
                "samples.$.Common_Name": sample['Common_Name'],
                "samples.$.Anonymized_Name": sample["Anonymized_Name"],
            }}
        )

Can someone tell me what is wrong with this please?

shaw2thefloor
  • 600
  • 5
  • 20

1 Answers1

1

Take a look at your update statement, where you have specified the $set modifier with redundant :

...{'$set:': {...

Just remove it, and all will work :)

bagrat
  • 7,158
  • 6
  • 29
  • 47