Slow performance on simple query using MongoDB ATLAS

I had around 2000 objects in this collection, a sample object:

image

I want to extract all object db.collection.find({}) with selected field .select using Mongoose :

    console.time('Execution Time');
    const events = await Events.find({}).select(
      "-_id -id -expireAt -homeNmae -awayName -competition"
    );
    console.timeEnd('Execution Time');

The Execution Time is more than 2s everytime, it’s pretty slow in this small collection. May I ask why and how I can improve.

.explain():

{
  explainVersion: '1',
  queryPlanner: {
    namespace: 'foodball.events',
    indexFilterSet: false,
    parsedQuery: {},
    queryHash: '17830885',
    planCacheKey: '17830885',
    maxIndexedOrSolutionsReached: false,
    maxIndexedAndSolutionsReached: false,
    maxScansToExplodeReached: false,
    winningPlan: {
      stage: 'PROJECTION_DEFAULT',
      transformBy: [Object],
      inputStage: [Object]
    },
    rejectedPlans: []
  },
  executionStats: {
    executionSuccess: true,
    nReturned: 1396,
    executionTimeMillis: 5,
    totalKeysExamined: 0,
    totalDocsExamined: 1396,
    executionStages: {
      stage: 'PROJECTION_DEFAULT',
      nReturned: 1396,
      executionTimeMillisEstimate: 1,
      works: 1397,
      advanced: 1396,
      needTime: 0,
      needYield: 0,
      saveState: 1,
      restoreState: 1,
      isEOF: 1,
      transformBy: [Object],
      inputStage: [Object]
    },
    allPlansExecution: []
  },
  command: {
    find: 'events',
    filter: {},
    projection: {
      _id: 0,
      id: 0,
      expireAt: 0,
      homeNmae: 0,
      awayName: 0,
      competition: 0
    },
    '$db': 'foodball'
  },
  serverInfo: {
    host: 'ac-omjpzdm-shard-00-02.tmhozcd.mongodb.net',
    port: 27017,
    version: '6.0.8',
    gitVersion: '3d84c0dd4e5d99be0d69003652313e7eaf4cdd74'
  },
  serverParameters: {
    internalQueryFacetBufferSizeBytes: 104857600,
    internalQueryFacetMaxOutputDocSizeBytes: 104857600,
    internalLookupStageIntermediateDocumentMaxSizeBytes: 16793600,
    internalDocumentSourceGroupMaxMemoryBytes: 104857600,
    internalQueryMaxBlockingSortMemoryUsageBytes: 33554432,
    internalQueryProhibitBlockingMergeOnMongoS: 0,
    internalQueryMaxAddToSetBytes: 104857600,
    internalDocumentSourceSetWindowFieldsMaxMemoryBytes: 104857600
  },
  ok: 1,
  '$clusterTime': {
    clusterTime: new Timestamp({ t: 1689649740, i: 11 }),
    signature: {
      hash: Binary.createFromBase64("V7X4bMZ0EByYM+mxZcxLQLBjdn0=", 0),
      keyId: new Long("7237192774783598597")
    }
  },
  operationTime: new Timestamp({ t: 1689649740, i: 11 })
}

P.S. I didn’t subscribe to any paid plan, will it affect the database performance?

Hi there,

Is there an index covering this query?

I assume “Execution Time” mentioned above is the time it takes to get the results back. This is different to the execution time on the server which appears to be 5ms:

executionStats: {
    executionSuccess: true,
    nReturned: 1396,
    executionTimeMillis: 5

I’m not familiar with the .time() you’ve noted in your code snippet but the 2 seconds you’ve mentioned sounds like the amount of time it takes the request to reach the server, execute, get the response back (possibly include other processes). This is probably going to include any network latency. Is the client you’re performing the query from on the same region as the atlas cluster?

Additionally, what’s the use case for returning all documents? i.e. Using {} in the query portion of the .find().

2 Likes

The problem is solved aftering I changing the databse region to my region(original wasn’t).
but there is few questions I wanna ask:

  1. Since I want to display a table which included all data in the collection, I think .find() should be the correct&fastest way to do it?
  2. How will the number of RAM of the Mongodb effect the efficiency? Is there any blog or stat to mention it?

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.