Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query for distinct results in mongodb with python?

I have a mongo collection with multiple documents, suppose the following (assume Tom had two teachers for History in 2012 for whatever reason)

{
"name" : "Tom"
"year" : 2012
"class" : "History"
"Teacher" : "Forester"
}

{
"name" : "Tom"
"year" : 2011
"class" : "Math"
"Teacher" : "Sumpra"
}


{
"name" : "Tom",
"year" : 2012,
"class" : "History",
"Teacher" : "Reiser"
}

I want to be able to query for all the distinct classes "Tom" has ever had, even though Tom has had multiple "History" classes with multiple teachers, I just want the query to get the minimal number of documents such that Tom is in all of them, and "History" shows up one time, as opposed to having a query result that contains multiple documents with "History" repeated.

I took a look at: http://mongoengine-odm.readthedocs.org/en/latest/guide/querying.html

and want to be able to try something like:

student_users = Students.objects(name = "Tom", class = "some way to say distinct?")

Though it does not appear to be documented. If this is not the syntactically correct way to do it, is this possible in mongoengine, or is there some way to accomplish with some other library like pymongo? Or do i have to query for all documents with Tom then do some post-processing to get to unique values? Syntax would be appreciated for any case.

like image 744
Rolando Avatar asked Aug 17 '12 14:08

Rolando


People also ask

How can I get distinct values from a field in MongoDB?

MongoDB – Distinct() Method In MongoDB, the distinct() method finds the distinct values for a given field across a single collection and returns the results in an array. It takes three parameters first one is the field for which to return distinct values and the others are optional.

How do I sort query results in MongoDB Python?

Use the sort() method to sort the result in ascending or descending order. The sort() method takes one parameter for "fieldname" and one parameter for "direction" (ascending is the default direction).

How do I get unique values in PyMongo?

PyMongo includes the distinct() function that finds and returns the distinct values for a specified field across a single collection and returns the results in an array. Parameters : key : field name for which the distinct values need to be found.

How do I run a distinct query in MongoDB compass?

You can do this via aggregation framework in Compass, using $unwind and $group. The $unwind is performed to create a unique document for each element in the target array, which enables the $addToSet operator in the $group stage to then capture the genres as distinct elements.


1 Answers

First of all, it's only possible to get distinct values on some field (only one field) as explained in MongoDB documentation on Distinct.

Mongoengine's QuerySet class does support distinct() method to do the job.

So you might try something like this to get results:

Students.objects(name="Tom").distinct(field="class")

This query results in one BSON-document containing list of classes Tom attends.

Attention Note that returned value is a single document, so if it exceeds max document size (16 MB), you'll get error and in that case you have to switch to map/reduce approach to solve such kind of problems.

like image 75
Rostyslav Dzinko Avatar answered Sep 22 '22 16:09

Rostyslav Dzinko