Mongoengine query set to list conversion
Asked Answered
T

1

5

As in django with sql backend we can convert a queryset into flat list by

foovar.objects.all().values_list('id', flat=true)

give a list of ids

How to get list of ids in mongo backend ,ORM being used is mongoengine in which values_list function has no flat parameter.

Tahsildar answered 3/8, 2015 at 6:15 Comment(0)
B
7

You are right, there is no flat parameter in values_list. But mongoengine has values_list. So simply stating:

foovar.objects.all().values_list('id')

returns all the id's of the foovar model.

Bark answered 3/8, 2015 at 6:44 Comment(8)
but it return list of <foovar>. you can check by type(foovar.objects.all().values_list('id')[0])Tahsildar
I want list of object ids only not list of foobar objects. Suggest any method to get list of ids as like flat=true param doesTahsildar
this works in my configuration. Actually mongoengine's 'id' field returns list of object ids. Here is a sample that is printed in my configuration. [ObjectId('53b4ecedd9bb3c486ce33977'), ObjectId('53bfac71d9bb3c4aea33011a'), ObjectId('5450b51d45027124f60f5d08'), ObjectId('54e3072b765351403be21921'), u'11825260', ObjectId('55a60540ac39214a5bdcea78'), ObjectId('55a6089bd9bb3c5445b3fac9'), ObjectId('55a790b6ac39216d8a889861'), ObjectId('55a790d8d9bb3c31627d4a6b'), ObjectId('55a8958ad9bb3c3d989ca0ff'), ObjectId('55a8c9c2d9bb3c7df4e115aa')]Bark
Can you tell me what version of mongoengine are you using?Bark
could check type(result[0]) and result[0].any_other_field, please inform me about that. PS: result= foovar.objects.all().values_list('id')Tahsildar
It is giving list of id's only in my configuration that is what I am saying.Bark
sure flask-mongoengine (0.7.1) and mongoengine (0.9.0)Tahsildar
Let us continue this discussion in chat.Bark

© 2022 - 2025 — McMap. All rights reserved.