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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:25:20+00:00 2026-06-11T14:25:20+00:00

I have two collections, as follows: db.ships document format: { mmsi: Long, …some other

  • 0

I have two collections, as follows:

db.ships
document format: { mmsi: Long, …some other fields }
indexes: { {mmsi: 1}, {unique: true} }

db.navUpdates
document format: { mmsi: Long, time: ISODate, …some other fields }
indexes: { mmsi: 1 }, { time: 1 }

For each document within db.ships, I need to find the most recent document within db.navUpdates that matches by mmsi. I cannot use _id to find most recent as documents are not necessarily entered in chronological (as defined by timestamp time) order.

E.g.:

ship document:
{ mmsi: 12345 }

navUpdate documents:
{ mmsi: 12345, time: ISODate("2012-09-19T12:00:00.000Z") }
{ mmsi: 12345, time: ISODate("2012-09-18T12:00:00.000Z") }
{ mmsi: 54321, time: ISODate("2012-09-19T12:00:00.000Z") }

So for the ship with mmsi:12345, the most recent navUpdate is the first document in the list above, with time:ISODate("2012-09-19T12:00:00.000Z").

I tried the following mongo shell script, but it’s incredibly slow (multiple seconds for just 10 queries), and messages appearing on the server indicate I’m missing an index.

db.ships.find().limit(10).forEach(function(ship) {
    var n = db.navUpdates.find({mmsi:ship.mmsi}).count();
    if (n==0) { return; }
    var t = db.navUpdates.find({mmsi:ship.mmsi}).sort({time:-1}).limit(1)[0].time;
    print(t);
});

Why is this query so slow? I tried adding a {time: -1} index to navUpdate, thinking perhaps the sort({time: -1}) might be the culprit, but still no improvement.

Also, can this query be optimized? I have the count() call in there because there are some ship documents whose mmsis are not found within navUpdates.

  • 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-11T14:25:21+00:00Added an answer on June 11, 2026 at 2:25 pm

    A single query can only use one index, so you should add a compound index of { mmsi: 1, time: -1 } to navUpdates that can be used for both your find and sort needs.

    Then use .explain() to determine if your indexes are being used in your queries.

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

Sidebar

Related Questions

I have a class which has two HashSet<String> collections as private members. Other classes
Suppose I have two collections as follows: Collection1: A1 A1 M1 M2 Collection2: M2
I have two collections as below which hold IDs for Students. The ids are
I have two generic collections in my code, EventableCollection and EventableSortedList - which are
I have a mongo collection which has documents with two fields fieldA and fieldB,
I have a statement as follows getLD().get(cam.getName()).getAGS().get(aG.getName()) getLD(), getAGS() return Java collections I would
I have two classes as follows: public class Info { [XmlAttribute] public string language;
I'm working on a control where you'd have two multi-select lists with collections of
I have two arraylist filelist and imgList which related to each other, e.g. H1.txt
I have a class with two date fields say: class TestData { Date activation;

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.