user11553898
user11553898

Reputation:

MondoDB/Mongoose query response is too slow

I am new to MongoDB/Mongoose, and work with a very large database (more than 25000 docs). I need to configure different queries: by fields, first 10 docs, one by id. The problem is with performance - the server response is too slow (about 10-15 seconds). Please tell me how to configure this so that the server response is fast? Does it depend only on the schema settings or on other things, such as database connection parameters, or query parameters?

P.S. Queries should be by 'district' and 'locality'. Thanks for any help!

Here is the schema:

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const houseSchema = new Schema({
  code: {
    type: String,
    required: false
  },
  name: {
    type: String,
    required: true
  },
  district: {
    type: String,
    required: true
  },
  locality: {
    type: String,
    required: false
  },
  recountDate: {
    type: Date,
    default: Date.now
  },
  eventDate: {
    type: Date,
    default: Date.now
  },
  events: {
    type: Array,
    default: []
  }
});

module.exports = mongoose.model('House', houseSchema);

Connection parameters:

mongoose.connect(
  `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0-vuauc.mongodb.net/${process.env.MONGO_DB}?retryWrites=true&w=majority`,
  {
    useNewUrlParser: true,
    useUnifiedTopology: true
  }
).then(() => {
  console.log('Connection to database established...')
  app.listen(5555);
}).catch(err => {
  console.log(err);
});

Queries are performed using Relay:

query {
  viewer {
    allPosts (first: 10) {
      edges {
        node {
          id
          code
          district
          locality
          recountDate
          eventDate
          events
        }
      }
    }
  }
}

Upvotes: 5

Views: 5954

Answers (3)

Ankit Kumar Rajpoot
Ankit Kumar Rajpoot

Reputation: 5590

You can read for Query optimize here

Use Projections to Return Only Necessary Data When you need only a subset of fields from documents, you can achieve better performance by returning only the fields you need:

For example, if in your query to the posts collection, you need only the timestamp, title, author, and abstract fields, you would issue the following command:

db.posts.find( {}, { timestamp : 1 , title : 1 , author : 1 , abstract : 1} ).sort( { timestamp : -1 } ).limit(10)

Upvotes: 2

Valijon
Valijon

Reputation: 13093

Make sure it's not a connection issue. Try to run your query from MongoDB shell

mongo mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0-vuauc.mongodb.net/${process.env.MONGO_DB}?retryWrites=true&w=majority
db.collection.find({condition}).limit(10)

If in MongoDB shell it responds faster than Mongoose:

There is an issue for Node.js driver which uses pure Javascript BSON serializer which is very slow to serialize from BSON to JSON.

Try to install bson-ext

The bson-ext module is an alternative BSON parser that is written in C++. It delivers better deserialization performance and similar or somewhat better serialization performance to the pure javascript parser.

https://mongodb.github.io/node-mongodb-native/3.5/installation-guide/installation-guide/#bson-ext-module

Upvotes: 2

Rajneshwar Singh
Rajneshwar Singh

Reputation: 122

MongoDB is very fast in the execution of queries. But it also depends on how you write your query. For getting the first 10 documents and sort it descending order to the _id from a collection. You need to use limit & sort in your query.

db.collectionName.find({}).limit(10).sort({_id:-1})

Upvotes: 4

Related Questions