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?

2
  • What is the identifier sample referring to, e.g. in sample['Source_Name']? Commented Mar 31, 2015 at 15:27
  • sample is a python dict, decoded from a json string using jsonpickle. Commented Apr 1, 2015 at 9:31

1 Answer 1

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 :)

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

Comments

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.