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

  • Home
  • SEARCH
  • 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 8729087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:46:18+00:00 2026-06-13T08:46:18+00:00

There are a few posts here about this but not exactly what I’m after.

  • 0

There are a few posts here about this but not exactly what I’m after.

I have a document that contains a list of embedded documents:

{
   "_id": 1234
   "name": "joe"
   "comments": [
       {"type": "text", "content": "my content"},
       {"type": "image", "content": "my_content"}
       {"type": "image", "content": "my_content"}
   ]

}

I want to run one query that gets a set of documents then I was hoping to run a secondary query to search the “comments” list from that initial query set.

e.g p = db.people.find({"some":"condition"}) and then search the embedded docs like p.find({"type":"image"}

This obviously doesn’t work. Just wondering if there’s a way to do this without having to run 2 separate queries over the parent document collection again?

  • 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-13T08:46:19+00:00Added an answer on June 13, 2026 at 8:46 am

    If you want to just find the items in a collection that satisfy a given condition and also satisfy {"type": "image"} in the comments array, you can do that in a single query:

    p = db.people.find({"some": "condition", "comments.type": "image"})
    

    See the dot notation page for more info.

    If you do actually need the whole thing, and you’re interested in specific sub-items from said result, then the best method that I can think of is just to slurp in the result into a list, and check it in Python. Do any PyMongo gurus have anything to say on this? Note that, if your dataset is large, then this is not a good idea.

    p = list(db.people.find({"some": "condition"})
    
    # A little verbose, but...
    image_p = [item for item in p
               if any(comment['type'] == 'image'
                      for comment in item['comments'])]
    
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are a few posts about this, but after hours of searching I still
There are a few threads here at so about this matter but most of
Hey I know there are already a few posts about this - yet I
Hey there, I have read the few posts here on when/how to use the
I have an application that's locked down using forms authentication. There are few nightly
There are a few windows service projects in our codebase that follow this model,
While there are already quite a few posts about leaks around UIWebView, mine is
There has been quite a few image-to-isolated-storage questions here, but I couldn't find a
I have a webpage where there are few text fields to be filled up
I know how to use locks in my app, but there still few things

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.