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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:00:50+00:00 2026-05-26T05:00:50+00:00

(Sorry if it’s a trivial question.) I have documents that looks like this (Python

  • 0

(Sorry if it’s a trivial question.)

I have documents that looks like this (Python syntax):

{
  '_id': SomeObjectId,
  'features': [ 
                {'id': 'featureX' , 'value': 6},
                {'id': 'featureY', 'value': 45}
              ]
}

With this structure it’s easy to find all documents that contains ‘featureX’ in the list of features. But I’m also interested in retrieving the value associated in the sub-document.
I think in Python if I get the document with a query like this: db.articles.find({'features.id': 'featureX'}) then I would need to iterate over the array ‘features’ to find out the correct ‘value’.

Is there an other kind of query that can give me the interesting value (in this case I don’t need to retrieve the full document, only the part with {‘id’: ‘featureX’, ‘value’: 6}, which won’t be at a predictable index value in the array.

PS: I think I will design the document differently, but I’m still interested to know if the resquest above is feasible.

Here is the new structure:

{
'_id': SomeObjectId,
'features': { 
              'featureX': { 'someproperty':'aaa', 'value':6 }, 
              'featureY': {'someproperty' :'bbb', 'value':45} 
            }
}

Is there any issue about this desgin ?
In my case ‘featureX’, ‘featureY’ are unique so using them as dictionnary keys is not a problem.

Edit:
I need to clarify that I will need to update atomically say ‘featureX’ and ‘featureY’ in a document and MongoDB does not support transactions.
Also the second design, while not allowing to retrieve a subdocument, should makes it easy to get quickly the desired information in the client code, assuming that I can query for sub-documents having a certain key.

I think that this query should do the job:

result = db.articles.find_one({ 'features.featureX': {'$exists': True} } )
interesting_value = result['features']['featureX']['value']
  • 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-05-26T05:00:51+00:00Added an answer on May 26, 2026 at 5:00 am

    I have answered this couple of times about picking up the sub-documents alone from mongo collection here, and here

    Simply there is no way to do this currently. This is the behavior of filtering multi level embedded document, normally the matching filter would return the whole document, not the subsets.

    There are two outstanding issues already in mongo related to this positional ($) operator in fields to return specifier and Ability to make use of a subdocument’s data whose contents were used to satisfy a query using the $ operator. (Please login to vote if you really needed the feature)

    And your alternate schema also not useful here.

    so you have to store the each feature in separate document like this to make it work the way you wanted

    feature 1

    {
    '_id': SomeObjectId,
    'name' :'some name',
    'value': 'feature 1',
    'some_field' : 'zzz'
    }
    

    feature 2

    {
    '_id': SomeObjectId,
    'name' :'some name',
    'value': 'feature 2',
    'some_field' : 'zzz'
    }
    

    and querying

    db.features.find({'_id':someobjectid})
    

    will only return the specific feature

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

Sidebar

Related Questions

Sorry if this sounds like a really stupid question, but I need to make
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Sorry this is basic, but I'm new to Python and Django. I have a
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry for the second newbie question, I'm a developer not a sysadmin so this
Sorry if this is a noob question, but All my code is error-free and
Sorry for puting this silly question: I am reading the book Using R for
Sorry about this newbie question. Is there a way to execute multiple sentences at

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.