-1

Alright so using Python and MongoDB I am trying to embed a subdocument within an array with a custom key value in the array. I was playing around with all sorts of different ways to do this and I couldn't figure out what I was doing wrong so I temporarily settled on the working code below. Numerous attempts always lead to the error:

in _check_write_command_response raise OperationFailure(error.get("errmsg"), error.get("code"), error) pymongo.errors.OperationFailure: The dotted field 'first.rule' in 'followedBy..first.rule' is not valid for storage.

Code:

 citizens.update(
    {"_id" : userPush},
    {"$push": {"followedBy":[field[1], field[2], field[3], field[0]]}})

Produces:

 "_id" : ObjectId("5…asfd"), 
            "uName" : "tim0", 
            "fName" : "tim",
            "lName" : "lost",
            "pic" : null, 
            "bio" : "I <3 MongoDB", 
            "followedBy" : [
                [
                    "BobTheBomb", 
                    "bobby", 
                    "knight", 
                    NumberInt(2)
                ], 
                [
                    "Robert", 
                    "DROP", 
                    "TABLE", 
                    NumberInt(6)
                ]

This is what I want:

"_id" : ObjectId("5…asfd"), 
    "uName" : "tim0", 
    "fName" : "tim",
    "lName" : "lost",
    "pic" : null, 
    "bio" : "I <3 MongoDB", 
    "followedBy" : [
            "BobTheBomb": { 
                    "fName" : "bobby", 
                    "lName" : "knight", 
                    "uID" : NumberInt(2)
        }, 
            "Robert": { 
                    "fName" : " DROP ", 
                    "lName" : " TABLE ", 
                    "uID" : NumberInt(6)
        }
    ]
user3586062
  • 1,289
  • 2
  • 10
  • 12

1 Answers1

0

You will need to build that data structure, currently you are saying that "followedBy" is only a list.

so try:

citizens.update(
    {"_id" : userPush},
    {"$push": {"followedBy":{field[1]: { "fName":field[2], "lName":field[3], "uID":field[0]}}}})

Remove the list and replace it with a dict.

I do hope this helps.


I have realised that I have not given you valid json, I have tested this:

citizens.update(
    {"_id" : userPush},
    {$push: 
    {"followedBy":
        [
            {field[1]: 
                { "fName": field[2], "lName": field[3], "uID": field[0]}
            }
        ]
    } 
})

And it worked...


You might find that the error is caused by the modifier you are using, I found the following on a blog:

MongoDB provides several different modifiers you can use to update documents in place, including the following (for more details see updates):

- $inc Increment a numeric field (generalized; can increment by any number)
- $set Set certain fields to new values
- $unset Remove a field from the document
- $push Append a value onto an array in the document
- $pushAll Append several values onto an array
- $addToSet Add a value to an array if and only if it does not already exist
- $pop Remove the last (or first) value of an array
- $pull Remove all occurrences of a value from an array
- $pullAll Remove all occurrences of any of a set of values from an array
- $rename Rename a field
- $bit Bitwise updates

you might find that because you are inserting many items that you would rather want to use $pushAll or $addToSet rather than $push... Just a speculation...

Renier
  • 1,523
  • 4
  • 32
  • 60
  • It returned "pymongo.errors.OperationFailure: The dotted field 'first.rule' in 'followedBy..0.first.rule' is not valid for storage." I kept running into that error early, as well as several other when I tried tweaking it. – user3586062 Mar 12 '15 at 07:45
  • That produces the literal strings "field[1]". Output: "followedBy" : [ { "field[1]" : { "uID" : "field[0]", "fName" : "field[2]", "lName" : "field[3]" } – user3586062 Mar 12 '15 at 07:59
  • oh sorry about that.. I used the string values to test the output - try the updated one – Renier Mar 12 '15 at 08:21
  • No you are fine, I really appreciate the help. I tried the updated code and it is still giving me the stubborn error: "in _check_write_command_response raise OperationFailure(error.get("errmsg"), error.get("code"), error) pymongo.errors.OperationFailure: The dotted field 'first.rule' in 'followedBy..first.rule' is not valid for storage." I am going to go to bed and give it another shot tomorrow. Thank you for the help – user3586062 Mar 12 '15 at 08:26
  • Still no luck, I am stuck on the same error and I do not understand the error – user3586062 Mar 13 '15 at 05:56
  • check out these questions: http://stackoverflow.com/q/12784423/2173793, http://stackoverflow.com/q/19709624/2173793, http://stackoverflow.com/q/13894769/2173793, http://stackoverflow.com/questions/6041109/mongo-update-query-given-error. maybe one of them will help solve your issue. and can you please update your question with the full stacktrace? – Renier Mar 13 '15 at 08:34