[MongoMapper] Remove an attribute

341 views
Skip to first unread message

jmonteiro

unread,
Apr 30, 2010, 7:57:35 AM4/30/10
to MongoMapper
Hello,

I would like to remove an attribute from an object saved as a
MongoMapper::Document.

In the MongoDB Developer FAQ (http://www.mongodb.org/display/DOCS/
Developer+FAQ), there is the following statement:

> If you remove an object attribute is it deleted from the store?
> Yes, you remove the attribute and then re-save() the object.

Thank you,
Julio

--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en

Andrew Timberlake

unread,
Apr 30, 2010, 8:05:38 AM4/30/10
to mongo...@googlegroups.com
You can use obj.unset(:key_name)

Andrew Timberlake
When you can't be on your bike
http://www.cyclemates.com

John Nunemaker

unread,
Apr 30, 2010, 1:54:07 PM4/30/10
to mongo...@googlegroups.com
I absolutely love that you all answer before I can. Saves me a lot of work. Thanks for helping out on the list everyone!

John Nunemaker

unread,
Apr 30, 2010, 1:55:44 PM4/30/10
to mongo...@googlegroups.com
One addition, you can also remove a key from all fields like this:

Foo.unset({}, :some_key)

Or just those that match particular criteria:

Foo.unset({:tags => 'red'}, :some_key)

On Fri, Apr 30, 2010 at 5:05 AM, Andrew Timberlake <and...@andrewtimberlake.com> wrote:

Adilson Chacon

unread,
Apr 30, 2010, 2:05:39 PM4/30/10
to mongo...@googlegroups.com
"unset" works for Joint too?
--
Adilson Chacon
site: http://www.adilsonchacon.com
twitter: http://www.twitter.com/adilsonchacon
email: adilso...@gmail.com

John Nunemaker

unread,
Apr 30, 2010, 4:35:07 PM4/30/10
to mongo...@googlegroups.com
Yeah, it could. You would need to do it for each of the 4 keys and then would also want to remove the files from GridFS.

Adilson Chacon

unread,
Apr 30, 2010, 4:38:07 PM4/30/10
to mongo...@googlegroups.com
OK, thanks!

Dan Gal

unread,
Apr 21, 2015, 8:11:48 AM4/21/15
to mongo...@googlegroups.com, juliom...@gmail.com
how to unset from embedded document? it doesn't seem to work...

Jamie Orchard-Hays

unread,
Apr 21, 2015, 10:21:15 AM4/21/15
to mongo...@googlegroups.com, juliom...@gmail.com
Should just be

parent_model_instance.unset("path.to.embedded_attr").

Or just drop to the driver:

ParentModel.collection.update({your query}, {:$unset => {"path.to.embedded_attr" => true}})



---
You received this message because you are subscribed to the Google Groups "MongoMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongomapper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dan Gal

unread,
Apr 22, 2015, 12:59:39 AM4/22/15
to mongo...@googlegroups.com, juliom...@gmail.com
Thanks for the quick reply but my embedded document is of type 'many' (array). 
for example I have the parent 'package' which has an embedded document of type many named 'page'. How do I unset one of pages fields?

Jamie Orchard-Hays

unread,
Apr 22, 2015, 9:21:04 AM4/22/15
to mongo...@googlegroups.com, juliom...@gmail.com
You will have to drop to the driver for doing this in an embedded array. Check the mongodb docs on Arrays. I think there's a way to find the position of the doc in the Array and then alter values. It may take some digging around and experimenting. 
Reply all
Reply to author
Forward
0 new messages