cobrateam / flask-mongoalchemy

Flask support for MongoDB using MongoAlchemy.
https://pythonhosted.org/Flask-MongoAlchemy/
BSD 2-Clause "Simplified" License
217 stars 45 forks source link

How to use remove()? #36

Open holms opened 10 years ago

holms commented 10 years ago

This is not really issue, but a question. I'm seriously confused, or I'm getting old.. Through all documentation I've found about MongoAlchemy and flask-mongoalchemy, I just don't understand how to remove from DB something simple as for example this:

class User(db.Document):
    username      = db.StringField()
    password_hash = db.HashField()

and let's say there's already one row in db where username is 'holms'. How can I remove it?? I mean completely delete whole row with password_hash too. In real system I usually avoid to remove users, but this is for unit testing.

My attempt:

 users = User.query.filter(User.username=="holms")

----> 1 users.remove_all(User.username, ['holms'])

/var/www/msgapp/backend/lib/python2.7/site-packages/mongoalchemy/query.pyc in remove_all(self, qfield, *value)
    387     def remove_all(self, qfield, *value):
    388         ''' Refer to:  :func:`~mongoalchemy.update_expression.UpdateExpression.remove_all`'''
--> 389         return UpdateExpression(self).remove_all(qfield, *value)
    390
    391     def add_to_set(self, qfield, value):

/var/www/msgapp/backend/lib/python2.7/site-packages/mongoalchemy/update_expression.pyc in remove_all(self, qfield, *value)
    123     def remove_all(self, qfield, *value):
    124         ''' Atomically remove each value in ``value`` from ``qfield``'''
--> 125         return self._atomic_list_op_multivalue('$pullAll', qfield, *value)
    126
    127     def add_to_set(self, qfield, value):

/var/www/msgapp/backend/lib/python2.7/site-packages/mongoalchemy/update_expression.pyc in _atomic_list_op_multivalue(self, op, qfield, *value)
    147         qfield = resolve_name(self.query.type, qfield)
    148         if op not in qfield.valid_modifiers:
--> 149             raise InvalidModifierException(qfield, op)
    150         wrapped = []
    151         for v in value:

InvalidModifierException: Invalid modifier for QueryField field: $pullAll
users = User.query.filter(User.username=='holms')
users.remove()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-74-4b66f4960b06> in <module>()
----> 1 users.remove()

TypeError: remove() takes exactly 3 arguments (1 given)