Vasilis Skentos
Vasilis Skentos

Reputation: 566

delete list item in pymongo collection

I have a users collection in pymongo and flask and inside the users collection I have the field "ratings":[] which takes items as input and appends them inside the list . I'm a beginner in pymongo and flask and I have trouble deleting a specific item inside the ratings list . Let's say that I have a user instance like :

user = users.find_one({"name":"Bill" , "ratings":["good" , "bad"]}) 

Using :

user['ratings'].remove("bad")

returns TypeError: string indices must be integers

How can I delete the "bad" item inside the ratings list and what does this error mean? I would appreciate your guidance with helping me with this task . Thank you in advance

Upvotes: 0

Views: 107

Answers (3)

ngShravil.py
ngShravil.py

Reputation: 5058

You should use the $pull operator.

db.collection.update(
  { name: 'Bill' },
  { $pull:
    { ratings: 'bad' }
  }
)

Upvotes: 2

Vasilis Skentos
Vasilis Skentos

Reputation: 566

SOLVED : users.update_one({"Email":email} , {"$pull":{"ratings":rating} }) did the job and deleted the user

Upvotes: 0

Shashank Shukla
Shashank Shukla

Reputation: 64

try this:

del user['ratings'][1]

Upvotes: 1

Related Questions