Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoengine query set to list conversion

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.

like image 523
Vaseem Ahmed Khan Avatar asked Aug 03 '15 06:08

Vaseem Ahmed Khan


People also ask

Which is better PyMongo or MongoEngine?

Both PyMongo and MongoEngine can be used to access data from a MongoDB database. However, they work in very different ways and offer different features. PyMongo is the MongoDB recommended library. It makes it easy to use MongoDB documents and maps directly to the familiar MongoDB Query Language.

How do I update MongoEngine?

MongoEngine provides the following methods for atomic updates on a queryset. update_one() − Overwrites or adds first document matched by query. update() − Performs atomic update on fields matched by query. modify() − Update a document and return it.

What is Python MongoEngine?

MongoEngine is a Python library that acts as an Object Document Mapper with MongoDB, a NOSQL database. It is similar to SQLAlchemy, which is the Object Relation Mapper (ORM) for SQL based databases.


1 Answers

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.

like image 156
Arpit Goyal Avatar answered Sep 28 '22 10:09

Arpit Goyal