Reputation: 27
I am trying to query a data set to return all list:string items not null. My example table:
db.define_table('newItems',
Field('name', 'string'),
Field('tasks', 'list:string'))
A query for None like so:
for item in db(db.newItems.tasks != None).select(db.newItems.tasks):
Returns:
[]
[]
[u'Wash Dog', u'Mow Yard']
[]
[u'Do X', u'Then do Y']
[]
How do I structure the query so that it only returns:
[u'Wash Dog', u'Mow Yard']
[u'Do X', u'Then do Y']
Thank you
update:
The answer works when tested on sqlite but it does not work with mongodb. The empty list [] is passed as '' that yields the wrong result:
In [4]: db.Tasks.email != []
Out[4]: <Query {'email': {'$ne': ''}}>
mongo shell:
> db.Tasks.find().count()
1195 (total records)
> db.Tasks.find({'email': {'$ne': ''}}).count()
1195
The correct query should be:
> db.Tasks.find({email: {$ne: [ ] }}).count()
108
Is this is a bug in mongo dal or is there another way I can specify the empty list?
Upvotes: 1
Views: 1251
Reputation: 25536
If you input records via SQLFORM
, empty values for task
will get stored as an empty list rather than None
, so you can do:
db(db.newItems.tasks != []).select(db.newItems.tasks)
Note, that will also exclude records that happen to have None
values for task
(which might exist if you have inserted records without task
values using .insert()
instead of a from).
UPDATE: Looks like the above doesn't work with MongoDB due to a bug in the MongoDB adapter.
Upvotes: 1