How efficient is searching by tags?

Viewed 362

Situation:

I have 1 million users and I want to search them by tags.

Here's the schema:

const Account = new Schema({
  tags: { type: [String], default: ['cats'] }
})

Here's the first query:

const query = { tags: 'cats'}
const fields = { _id: 0 }
const options = { skip: 0, limit: 5 }

Account.find(query, fields, options)

After calling find method Mongoose will start searching and return the first 5 entries it matches. The performance in this case is optimal.

Here's the second query:

const query = { tags: 'cats'}
const fields = { _id: 0 }
const options = { skip: 500 000, limit: 5 }

Account.find(query, fields, options)

What going on in this case is of interest to me.

Does Mongoose match 500 000 entries first and then returns the next 5 entries?

Or does it somehow 'jump' to the 500 000 element without having to match them all beforehand?

I'm trying to understand how efficient this proccess is and whether I can improve it somehow. Do you have any advice?

2 Answers
Related