Search code examples
pythonmongodbcherrypymongoengine

Deleting a row from an embedded document in mongo engine


I have a document called login_info. It has two fields:

  • name
  • password
  • Location

Location is an embedded document with two fields:

  • datetime
  • city

There are datas in the embedded document and i want to delete a row from it. for eg: i have to delete all the rows with location "Canada". How can i do this in mongo engine ? any help


Solution

  • Try this

    db.login_info.update({}, {$pull:{location:{city:"Canada"}}})
    

    Ok try this instead

    db.login_info.update( { "location.city" : { $exists : true } }, { $unset : { "location.city" : "Canada" } }, false, true);
    

    This will remove all "rows" where city is "Canada".