How does mongoose (Builder pattern) chained methods work under the hood

197 Views Asked by At

The question is more on the mongoose implementation of chained methods than actually mongo queries. I am working n a plugin implementation for which I need to provide custom methods like populate, count, etc. shown below.

a.find({ b: 'thing' }).populate('d').exec(etc..)

I am using the mongoose static methods to create the plugin specific method as shown below:

Schema.statics.byName = function(name) {
  return this.find({ name: name });
};

I am looking in the mongoose module repository, It is taking more time than I thought, I want to know how these chained methods are implemented. If one calls the find and populate chained as shown above, how to determines which methods are chained and handle them,

In the above example, the chained methods are find, populate, and exec. If anyone has an idea and will explain the way it works, It will be really helpful.

0

There are 0 best solutions below