Set default values in arrays

156 views
Skip to first unread message

Kefei Dan Zhou

unread,
Mar 7, 2011, 8:46:04 PM3/7/11
to MongoKit
I have a structure that looks like this. How do I set default values
and required fields for 'data' field inside the list. I tried
'items.data' and '$items.data' but neither work.

structure = {
"items": [ {
"id": int,
"data": unicode,
} ],
}

Nicolas Clairon

unread,
Mar 8, 2011, 5:09:44 AM3/8/11
to mong...@googlegroups.com
You may try something like that:

default_values = { 'items': [{'id': 3, 'data':'foo'}] }

glyphobet

unread,
Apr 27, 2011, 9:10:33 AM4/27/11
to MongoKit
I'm having the same problem, and the solution on this thread doesn't
work for me.

Here's my code:

@con.register
class UserInfo(Document):
use_dot_notation = True
structure = {
'email_addresses': [{
'email': unicode,
'verified': bool,
}],
}
required_fields = []
default_values = {
'email_addresses': [{
'verified': True,
}],
}

It raises this exception when I instantiate a new document:

ValueError: Error in default_values: can't find email_addresses in
structure

I've also tried 'email_addresses.verified': False, in default_values.
That fails differently, when I call save(), with:

mongokit.schema_document.StructureError: missed fields : ['verified']

I'm using MongoKit 0.6, from PyPi.

-matt


On Mar 8, 12:09 pm, Nicolas Clairon <clai...@gmail.com> wrote:
> You may try something like that:
>
> default_values = { 'items': [{'id': 3, 'data':'foo'}] }
>

Daniel Rodriguez del Villar Trimarchi

unread,
Mar 16, 2013, 7:44:23 PM3/16/13
to mong...@googlegroups.com
How to specify the field "data" to be required?

structure = { 
     "items": [ { 
            "id": int, 
            "data": unicode, 
        } ], 
}

required_fields = ["items.data"] # This doesn't work.

Reply all
Reply to author
Forward
0 new messages