How to remove key fields
I know开发者_如何学JAVA how to delete documents in a collection. i.e. User.last.delete
In the User
document. I have the following keys: first_name
, last_name
, gender
.
Lets say , I decide to remove the key last_name
. So I would have first_name
and gender
only. How would I go about this? Is this even possible?
I tried User.last.last_name.delete
to no avail.
Using mongoid: User.last.unset('last_name') should do the trick.
If you want to remove the last_name
field from the entire collection (from all the documents in the collection), you would do User.all.unset('last_name')
If you are actually trying to remove the column from the database, you should make a migration to remove the column.
If you are trying to change a record to not have a last name you could do something like User.last.last_name = false
and then save it.
精彩评论