Omitting optional fields on insert

81 views
Skip to first unread message

Andrei

unread,
Jun 9, 2010, 4:44:55 PM6/9/10
to MongoKit
I'm starting to play around with MongoKit and I have a question: is it
possible to omit inserting non-required fields when saving a new
document? For example, if my BlogPost has 'tags' field, but it's not
defined as required, MongoKit still inserts it into the database as
'tags':null. I'd like to avoid that, if possible. One of the
attractions of MongoDB is that it allows freeform document structure
and storing a null field just seems to harken back to SQL..

Nicolas Clairon

unread,
Jun 10, 2010, 9:24:27 AM6/10/10
to mong...@googlegroups.com
Not at this time but this is a great features. I added a case here
http://bitbucket.org/namlook/mongokit/issue/67/omitting-optional-fields.
Feel free to contribute to this proposal.

N.

Andrei

unread,
Jun 10, 2010, 10:17:22 AM6/10/10
to MongoKit
I tried to add a comment there, but the commenting/captcha system is
broken.

Anyway, why not just omit any field that is not in the requied_fields
list and also does not have a default value? A field like that could
only be inserted with a null value and that's exactly what we want to
prevent.

On Jun 10, 6:24 am, Nicolas Clairon <clai...@gmail.com> wrote:
> Not at this time but this is a great features. I added a case herehttp://bitbucket.org/namlook/mongokit/issue/67/omitting-optional-fields.

Nicolas Clairon

unread,
Jun 10, 2010, 10:40:10 AM6/10/10
to mong...@googlegroups.com
yep, this should do the trick... We still need an options, like
use_floating_fields=True, to specify the new behavior.

Andrei

unread,
Jun 10, 2010, 11:51:30 AM6/10/10
to MongoKit
Speaking of optional fields, I was trying to do something like this:

class User(Document):
structure = {
'username' : unicode,
'password' : unicode,
'reg_time' : int,
}
required_fields = ['username', 'password']
default_values = {'reg_time': timestamp_as_int}

initial = {'username':'foo', 'password':'bar'}
User(initial).validate()

It threw an exception saying that "reg_time" was missing, but I
expected the validation to pass because reg_time is an optional
field.

Andrei

unread,
Jun 15, 2010, 4:50:10 PM6/15/10
to MongoKit
Any thoughts on this?

On Jun 10, 8:51 am, Andrei <and...@zmievski.org> wrote:
> Speaking of optional fields, I was trying to do something like this:
>
> class User(Document):
>         structure = {
>                 'username' : unicode,
>                 'password' : unicode,
>                 'reg_time' : int,
>         }
>         required_fields = ['username', 'password']
>         default_values = {'reg_time': timestamp_as_int}
>
> initial = {'username':'foo', 'password':'bar'}
> User(initial).validate()
>
> It threw an exception saying that "reg_time" was missing, but I
> expected the validation to pass because reg_time is an optional
> field.
>
> On Jun 10, 7:40 am, Nicolas Clairon <clai...@gmail.com> wrote:
>
>
>
> > yep, this should do the trick... We still need an options, like
> > use_floating_fields=True, to specify the new behavior.
>
> > On Thursday, June 10, 2010,Andrei<and...@zmievski.org> wrote:
> > > I tried to add a comment there, but the commenting/captcha system is
> > > broken.
>
> > > Anyway, why not just omit any field that is not in the requied_fields
> > > list and also does not have a default value? A field like that could
> > > only be inserted with a null value and that's exactly what we want to
> > > prevent.
>
> > > On Jun 10, 6:24 am, Nicolas Clairon <clai...@gmail.com> wrote:
> > >> Not at this time but this is a great features. I added a case herehttp://bitbucket.org/namlook/mongokit/issue/67/omitting-optional-fields.
> > >> Feel free to contribute to this proposal.
>
> > >> N.
>

Nicolas Clairon

unread,
Jun 16, 2010, 5:24:18 AM6/16/10
to mong...@googlegroups.com
Hi,

Sorry for the delay, too many things to do before MongoFR...

For now, the only solution to do what you're expected is to disabled
the validation. But this is not what you want. I have to implement
this new behavior but I have to write it carefully as I don't want to
modify the current behavior (hopefully, Mongokit is full of tests).

So, please, just wait while this feature is implemented (you can track
the ticket here :
http://bitbucket.org/namlook/mongokit/issue/67/omitting-optional-fields)
or you can always send patches :-)

N.

Reply all
Reply to author
Forward
0 new messages