ListField questions

48 views
Skip to first unread message

emiscreant

unread,
Feb 28, 2012, 12:09:02 PM2/28/12
to MongoEngine Developers
I have two related ListField questions;

1. What is the best way to append additional EmbeddedDocumentFields to
a ListField in an already saved Document?
2. How can I count the number of items in a ListField?

Thanks for any help

Em

Ross Lawley

unread,
Feb 28, 2012, 12:17:42 PM2/28/12
to mongoen...@googlegroups.com
Hi,

1) The best way is just to use the push method eg:

new_embed = EmbeddedDoc(**kwargs)
MyDoc.objects(id=some_id).update(push__listfield=my_embed)

2) Counting - either return the document and do a len or denormalise and add a counter.

Ross

Em Iscreant

unread,
Feb 28, 2012, 12:38:19 PM2/28/12
to mongoen...@googlegroups.com
Ross,

Sweet!  Thanks so much the prompt and helpful reply.  Both solutions worked.

Are these issues in the documentation?  Perhaps I was using the wrong search approach, but I could not find the answers anywhere.

Thanks again

Em

Ross Lawley

unread,
Feb 29, 2012, 4:11:42 AM2/29/12
to mongoen...@googlegroups.com
Em,

I'll double check - the push operator is native to mongoDB though :)  The second is its just a list and as theres no way to compute that in the query language, so you have to use raw python, denormalising is just a trick you might want to use if your doing pagination and wanting to slice the ListField so not all of it is returned.

Ross
Reply all
Reply to author
Forward
0 new messages