Defaulting values of new fields on existing documents

449 views
Skip to first unread message

nader

unread,
Oct 19, 2010, 8:53:03 PM10/19/10
to Mongoid
Before I work on a patch, I wanted to get the community's opinion on
this.

I run into this issue all the time, where I add a new field to my
mongoid document definition with a default value. The default value
applies to new documents, but not to existing documents.

I think that if you set a default value and you request that field on
a document it should return the value even it didn't have it
originally.

What do you guys think?

James Harton

unread,
Oct 19, 2010, 8:57:55 PM10/19/10
to mon...@googlegroups.com
I would tend to agree with your thesis; this caused a lot of headaches for me recently. You can always make it a separate plugin if others don't like it.

James Harton
@jamesotron

Doug Mayer

unread,
Oct 19, 2010, 8:58:22 PM10/19/10
to mon...@googlegroups.com, nader
+1 for this behavior. We might want the ability to see if a particular
field is being defaulted (is it null in the DB?), or perhaps a way to
not default it, but I think what you describe would be the expected
behavior.

Jacques Crocker

unread,
Oct 19, 2010, 9:00:51 PM10/19/10
to mon...@googlegroups.com
This is a really good idea and something I personally need as well. However I think it work better as some sort of migration rake task.

rake mongoid:set_defaults

Something that scans through all the records for the association collection on each model and sets the default fields if not already set.

Andrew Coldham

unread,
Oct 19, 2010, 9:19:16 PM10/19/10
to mon...@googlegroups.com, mon...@googlegroups.com
+1 for Jacques method. Minimize surprises. (E.g. if you set the default value after a read, is the object dirty? It definitely needs to be saved.)

nader

unread,
Oct 20, 2010, 11:24:49 AM10/20/10
to Mongoid
Great. I agree with the Jacques method. Simple and surprise free.
I'll try and work on the rake task this weekend. If anyone wants to
help let me know.

On Oct 19, 7:19 pm, Andrew Coldham <andrewcold...@gmail.com> wrote:
> +1 for Jacques method. Minimize surprises. (E.g. if you set the default value after a read, is the object dirty? It definitely needs to be saved.)
>

Bradley Grzesiak

unread,
Oct 20, 2010, 11:30:24 AM10/20/10
to mon...@googlegroups.com
what about something like:

class Foo
  include Mongoid::Document
  include Mongoid::Migrations

  field :bar, :default => ''
end

class FooMigration
  migrate :bar, :from => nil
end

Perhaps no rake task would be necessary?

:brad
--
Bradley Grzesiak
co-founder, bendyworks llc
http://bendyworks.com/

nader

unread,
Oct 20, 2010, 7:39:56 PM10/20/10
to Mongoid
Brad, I prefer the rake task. You can just add it to your deploy
process and everything will be defaulted. I can see a case for adding
something to the field to indicate that you don't want to migrate that
field in existing docs though. But that would be the exception in
most cases.

nader

unread,
Oct 22, 2010, 8:03:35 PM10/22/10
to Mongoid
Hi everyone, here's the first draft of the rake task: http://gist.github.com/641569

Improvement suggestions very welcome.

Couple things to note.
1) I dropped down to the Ruby driver, because I don't know how to
update all the docs in a collection with one call using Mongoid.
2) This currently only works for top level documents. I don't think
there is a way to update all embedded docs without looping through the
individual parent documents? So maybe that is the better approach?
Seems like it will be a serious performance hit with a large set of
documents.

Jacques, where's the best place to put this? Just in the Rakefile?

Nader

nader

unread,
Oct 25, 2010, 2:27:19 PM10/25/10
to Mongoid
OK, Jacques I found the database.rake file, not sure how I missed that
earlier.

So here is the rake task: http://github.com/iamnader/mongoid/commit/ffb5a2b05202fe14e3b6b397382b02fcf402081f

Let me know if anyone has any feedback before I send the pull request.

I'll try and work on adding support for embedded documents today.

nader

unread,
Oct 27, 2010, 4:00:07 PM10/27/10
to Mongoid
OK, I got this working with embedded documents. Turned out to be more
complicated than I anticipated due to positional operators only
updating the first match and unexpected cases with deeply nested
documents (like if the nested document doesn't exist at all on some of
the parent docs.

Sent a pull request: http://github.com/mongoid/mongoid/pull/416



On Oct 25, 11:27 am, nader <iamna...@gmail.com> wrote:
> OK, Jacques I found the database.rake file, not sure how I missed that
> earlier.
>
> So here is the rake task:http://github.com/iamnader/mongoid/commit/ffb5a2b05202fe14e3b6b397382...

nader

unread,
Nov 22, 2010, 7:19:30 PM11/22/10
to Mongoid
Updated this with better exception handling. Still needs to be pulled
in
> > > > > > collection on each model and sets thedefaultfields if not already set.
>
> > > > > > > > On Oct 19, 2010, at 5:53 PM, nader wrote:
>
> > > > > > > >> Before I work on a patch, I wanted to get the community's opinion on
> > > > > > > >> this.
>
> > > > > > > >> I run into this issue all the time, where I add a new field to my
> > > > > > > >> mongoid document definition with adefaultvalue.  Thedefaultvalue
> > > > > > > >> applies to new documents, but not to existing documents.
>
> > > > > > > >> I think that if you set adefaultvalue and you request that field on

JDeville

unread,
Nov 25, 2010, 11:19:15 AM11/25/10
to Mongoid
Are you sure this is working w/ embedded documents? It's been
absurdly helpful for me so far, but I've got a problem now with it not
updating embedded schemas.

Here's what I have:

class User
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :interests, :class_name => "Interest"
end

class Interest
include Mongoid::Document
embedded_in :user, :inverse_of => :interests
identity :type => String
field :name
field :category
field :created_by, :default => "fb"

attr_accessible :name, :category, :_id, :created_by
end

The new field is :created_by, but the field isn't being created and
initialized.

Thanks
-Jeff
Reply all
Reply to author
Forward
0 new messages