Rob
Rob

Reputation: 3499

Delete Embedded Document from ListfField in Mongo Engine

I haven't found a clear answer to this yet, but the issue is I need to delete and/or update an Embedded Document in a listfield. So if I had a schema:

-Team
    -Players
        -name
        -number

how would I delete or update Players?

Upvotes: 1

Views: 356

Answers (1)

matino
matino

Reputation: 17735

It should look like this (note these are raw mongodb queries):

# To update the number:
db.team.update({ "name": "FC Barcelona", "players.name" : "Lionel Messi" }, { $set: { "players.$.number" : 11 }})

# To remove the player from the list:
db.team.update({ "name": "FC Barcelona", "players.name" : "Cesc Fabregas" }, { $pull: { "players.name" : "Cesc Fabregas" }})

Upvotes: 1

Related Questions