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 8445491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:40:28+00:00 2026-06-10T09:40:28+00:00

Good day everyone. Suppose we have a collection and a document which looks something

  • 0

Good day everyone.
Suppose we have a collection and a document which looks something like this:

    test_doc = {
    "ID" : "123",
    "a" : [
                    {
                        'x' : "/",
                        'y' : "2000",
                        'z' : "1000"
                    },
                    {
                        'x' : "/var",
                        'y' : "3500",
                        'z' : "3000"
                    }
           ]

      }

What i need is to retrieve a single property a.z .
In MongoDB i’m using the following query:

db.testcol.find({"ID":"123","a.x":"/"},{'a.z':1})

which returns this:

{ "_id" : ObjectId("skipped"), "a" : [ { "z" : "1000" }, { "z" : "3000" } ] }

As you can see it returns all the z properties, but i need only the first one or the second when condition is {"ID":"123","a.x":"/var"}
So, the question is: how do i get a single property in this situation? Is it just a matter of bad design or should i somehow process the returned document in code (python)? Any suggestions will be much appreciated.

  • 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-10T09:40:29+00:00Added an answer on June 10, 2026 at 9:40 am

    In MongoDB 2.0 and older, this is not possible. What you want to do is return a specific element of the array – but that is not what your projection is actually doing, it will just return the whole array and then the z element of each one.

    However, with 2.2 (rc2 as of writing this answer), things have gotten a bit better. You can now use $elemMatch as part of your projection (see SERVER-2238 for details) so that you only pull back the required array element. So, try something like this:

    db.foo.find({"ID":"123",'a':{$elemMatch:{'x':"/"}}},{_id : 0, 'a.$': 1})
    //returns
    { "a" : [ { "x" : "/", "y" : "2000", "z" : "1000" } ] }
    

    Or, just use $elemMatch in the projection itself, which you may think is cleaner:

    db.foo.find({"ID":"123"},{_id : 0, 'a':{$elemMatch:{'x':"/"}}})
    //returns 
    { "a" : [ { "x" : "/", "y" : "2000", "z" : "1000" } ] }
    

    So, now, at least the array returned is only the one containing only the entries you want and you can simply reference the relevant z element (elemMatch projections on a subdocument are not yet supported).

    Last but not least, in 2.2 we have the aggregation framework, and one of the things it can do (with the $project operator, is to reshape your documents and change sub documents and array elements into top level arrays. To get your desired result, you would do something like this:

    db.foo.aggregate( 
            {$match : {"ID":"123"}},  
            {$unwind : "$a"},  
            {$match : {"a.x":"/"}},  
            {$project : {_id : 0, z : "$a.z"}}
    )
    

    The result looks like this:

    { "result" : [ { "z" : "1000" } ], "ok" : 1 }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day everyone, i have faced with such an issue as linkage error like
Good Day Everyone, I have a program (lets call it 'A'), which is called
Good day everyone. This problem was part of another one which it as been
Good Day Everyone... I’m getting an unexpected WCF error complaining of Known Types which
Good day everyone. I have been having the same problem all day at work
Good day everyone! I have a problem regarding my date. It needs to be
Good day everyone, this is one of those areas that is a little over
Good day everyone. I have a question about making and using derived classes of
Good day everyone. I have an audio class, that plays a .wav file. But
Good day, I have a gen_server process which does some long-running state-updating tasks periodically

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.