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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:11:38+00:00 2026-06-15T16:11:38+00:00

My JSON documents (called i), have sub documents (called elements). I am looping trhough

  • 0

My JSON documents (called “i”), have sub documents (called “elements”).
I am looping trhough these subdocuments and updating them one at a time. However, to do so (once the value i need is computed), I have mongo scan through all the documents in the database, then through all the subdocuments, and then find the subdocument it needs to update.

I am having major time issues, as I have ~3000 documents and this is taking about 4minutes.
I would like to know if there is a quicker way to do this, without mongo having to scan all the documents but by doing it within the loop.

Here is the code:

for i in db.stuff.find():

    for element in i['counts']:

        computed_value = element[a] + element[b]
        db.stuff.update({'id':i['id'], 'counts.timestamp':element['timestamp']}, 
                        {'$set': {'counts.$.total':computed_value}})

I am identifying the overall document by “id” and then the subdocument by its timestamp (which is unique to each subdocument). I need to find a quicker way than this. Thank you for your help.

  • 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-15T16:11:39+00:00Added an answer on June 15, 2026 at 4:11 pm

    What indexes do you have on your collection ? This could probably be sped up by creating an index on your embedded documents. You can do this using dot notation — there’s a good explanation and example here.

    In your case, you’d do something like

    db.stuff.ensureIndex( { "i.elements.timestamp" : 1 });
    

    This will make your searches through embedded documents run much faster.

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

Sidebar

Related Questions

I have an aspx page that returns valid JSON - however when called via
I am currently asynchronously fetching json documents to update elements in a few html
i have two files one called stats.js one called storage.html in stats.js in contains
I have two functions that make $.getJSON calls - one looks at JSON stored
I have 41 JSON objects, each with the same scheme. These objects are fairly
Is it possible to merge two JSON documents with the Jackson JSON library? I
I'm implementing a service where each user must have his own json/document database. Beyond
I have the following simple jquery snippet $(document).ready(function () { $.ajax({ url:myjson.json, dataType: 'json',
i have been having this trouble when trying to obtain json data. lets say
I have the following jQuery event that gets fired every time an anchor is

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.