One interesting feature of my model is the ability to chain queries like find(), limit(), and skip(). However, there arises a question: How can I apply the limit or skip function to the output of Model.find() if the returning value does not inherently contain these functions?
For instance, consider the following code:
const mongoose = require("mongoose");
const password = encodeURIComponent("*****");
const username = encodeURIComponent("*****");
async function main() {
await mongoose.connect(`mongodb+srv://${username}:${password}@cluster0.e5rojbd.mongodb.net/test?ssl=true&retryWrites=true&w=majority`);
const userSchema = new mongoose.Schema({
name: String
});
const Name = mongoose.model('names', userSchema);
const limitedNames = Name.find().limit(2).skip(1);
console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(limitedNames)))
// output is [ 'constructor', '_queryMiddleware' ]
}
main().catch(err => console.log(err));
This code successfully applies limit and skip functions, but how is it achieved?