Passing atributes in variables to $ operators

17 views
Skip to first unread message

nabil

unread,
Feb 6, 2012, 11:07:18 PM2/6/12
to MongoEngine Users
Hi,

How can I pass document attributes in variables to operators like
$set? I've tried this, but it looks like the __raw__ attribute is not
supported in update_one().

class Part(Document):
name = StringField()

foo = 'name'
# this should update the name attribute of the Part doc
Part.objects(...).update_one(__raw__={'$set' : {foo : 'bar'}})

Also, where can I found documentation on the underscore syntax?

Thanks

Ross Lawley

unread,
Feb 7, 2012, 6:59:36 AM2/7/12
to mongoeng...@googlegroups.com
Hi,

You can use update_one(set__FIELDNAME=value) - theres some examples here: 


Hope that helps

Ross

nabil

unread,
Feb 8, 2012, 9:41:37 AM2/8/12
to MongoEngine Users
What if FIELDNAME is in a variable like in my example?

Ross Lawley

unread,
Feb 8, 2012, 9:49:48 AM2/8/12
to mongoeng...@googlegroups.com
Hi,

Well then you can just pass it as a kwarg eg: **{"set__%s" % VAR: value}

Ross

nabil

unread,
Feb 13, 2012, 10:59:50 PM2/13/12
to MongoEngine Users
Thanks. Forgot about kwargs.
Reply all
Reply to author
Forward
0 new messages