Question

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?

Was it helpful?

Solution

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 .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top