Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:31:02+00:00 2026-06-09T20:31:02+00:00

I have a mongo collection with multiple documents, suppose the following (assume Tom had

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:31:04+00:00Added an answer on June 9, 2026 at 8:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mongo collection which has documents with two fields fieldA and fieldB,
I have a collection of the following sort of documents (taken from Mongo DB
I have a Mongo collection called documents which has the following structure: { name:
I have an existing mongo database where all documents in a collection have a
I have a number of different documents in a mongo collection. The attrs are
I have a lot of updates in one mongo collection and assume huge fragmentation
I have a very basic question on updating a mongo collection. Suppose I have
I have a collection in mongo in which I am inserting data from Perl
Just started mongo and started having issue with querying already. i have a collection
I have many collections of documents in a mongo database that look like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.