1

Say i have this database "list", which contains a collection called "users", which contain, among others, an object called "david"

{u'_id': u'david', u'url': u'url3', u'old_url': u'url3', u'wishlist': [[u'Jenara', u'shards', u'nm'], [u'force of will', u'mm2', u'nm'], [u'pact of negation', u'mm', u'nm'], [u'all is dust', u'mm4', u'nm']]}

how can i use pymongo to edit the arrays within the wishlist field? say i want to remove one of the four arrays, or edit one of them?

Community
  • 1
  • 1
David Spira
  • 153
  • 2
  • 16

1 Answers1

1

In order to update an element in an array, use $set. Here is an example - updating the second element and setting it's value to ["something", "else"]:

db.users.update({'_id': 'david'}, {"$set": {"wishlist.1": ["something", "else"]}})

As for the removing an item from an array by index, it's not that easy and straightforward, see:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195