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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:13:16+00:00 2026-06-10T14:13:16+00:00

I would like to speed up an query on my mongoDB which uses $where

  • 0

I would like to speed up an query on my mongoDB which uses $where to compare two fields in the document, which seems to be really slow.

My query look like this:

db.mycollection.find({ $where : "this.lastCheckDate < this.modificationDate})

What I would like to do is add a field to my document, i.e. isCheckDateLowerThenModDate, on which I could execute a probably much faster query:

db.mycollection.find({"isCheckDateLowerThenModDate":true})

I quite new to mongoDB an have no idea how to do this. I would appreciate if someone could give me some hints or examples on

  1. How to initialize such a field on an existing collection
  2. How to maintain this field. Which means how to update this field when lastCheckDate or modificationDate changes.

Thanks in advance 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-10T14:13:19+00:00Added an answer on June 10, 2026 at 2:13 pm

    You are thinking in a right way!

    1.How to initialize such a field on an existing collection.

    Most simple way is to load each document (from your language), calculate this field, update and save.

    Or you could perform an update via mongo shell:

    db.mycollection.find().forEach(function(doc) { 
       if(doc.lastCheckDate < doc.modificationDate)
       {
         doc.isCheckDateLowerThenModDate = true;
       }
       else
       {
         doc.isCheckDateLowerThenModDate = false;
       }
       db.mycollection.save(doc); 
    });
    

    2.How to maintain this field. Which means how to update this field when
    lastCheckDate or modificationDate changes.

    You have to do it yourself from your client code. Make some wrapper for update, save operations and recalculate this value each time there. To be absolutely sure that this update works — write unit tests.

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

Sidebar

Related Questions

I would like to speed a MySQL query that basically retrieve a page of
I would like to check the load speed of each page in a particular
I would like to know if there is a difference in speed between computing
I am just coming up to speed on WPF and would like to create
Would like someone to take a look at my script and tell me where
I would like to write a query that simply returns 1 or 0 depending
the query i'd like to speed up (or replace with another process): UPDATE en_pages,
First, I would like to know if this query is Lazy loading or Eager
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
Would like a for loop in jquery so that: For every hover_link: show hidden

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.