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.

1 Answers1

6

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.

Arpit Goyal
  • 2,212
  • 11
  • 31
  • but it return list of . you can check by type(foovar.objects.all().values_list('id')[0]) – Vaseem Ahmed Khan Aug 03 '15 at 06:54
  • 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 does – Vaseem Ahmed Khan Aug 03 '15 at 06:58
  • 1
    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')]` – Arpit Goyal Aug 03 '15 at 06:59
  • Can you tell me what version of mongoengine are you using? – Arpit Goyal Aug 03 '15 at 07:01
  • could check type(result[0]) and result[0].any_other_field, please inform me about that. PS: result= foovar.objects.all().values_list('id') – Vaseem Ahmed Khan Aug 03 '15 at 07:03
  • It is giving list of id's only in my configuration that is what I am saying. – Arpit Goyal Aug 03 '15 at 07:03
  • sure flask-mongoengine (0.7.1) and mongoengine (0.9.0) – Vaseem Ahmed Khan Aug 03 '15 at 07:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84975/discussion-between-arpit-goyal-and-user2108368). – Arpit Goyal Aug 03 '15 at 07:07