Coder1
Coder1

Reputation: 13321

pymongo: Add field containing a dict

Right now my mongo document has normal key/value pairs in it.

I want to add a field (staff) that is a dict. I can run this code, and it doesn't complain, but the users field does not show up when I find the object. The object shows up as if it was never added.

staff = {'foo':{'name':'Jack S.', 'title':'Pirate'}, 'bar':{'name':'Abe', 'title':'Mate'}}
ships.update(
  {'_id': 1}, 
  {"$set": {'staff': staff},
  upsert=False
) 

My _id is definitely correct because I can find on that ID and I get the result without staff just fine.

How can I add this field to my existing collection?

Upvotes: 0

Views: 1463

Answers (1)

gsagrawal
gsagrawal

Reputation: 3020

try

ships.update(
  {'_id': 1}, 
  {"$set": {'staff': staff}},
  false,true
) 

** you were missing closing bracket. also your staff variable doesnt seems to a correct dictionary.it looks like list of dict. please check .

Upvotes: 1

Related Questions