What is the use of mongoose methods and statics?

后端 未结 2 1552
猫巷女王i
猫巷女王i 2021-01-30 21:20

What is the use of mongoose methods and statics and how are they different from normal functions?

Can anyone explain the difference with example.

2条回答
  •  执念已碎
    2021-01-30 21:57

    If i wanted to retrieve animals with hasTail i could simply change this line of code:

    return this.model('Animal').find({ type: this.type }, cb);
    

    to:

    return this.model('Animal').find({ hasTail: true }, cb);
    

    and i would not have to create a statics function.

    Use method on individual documents if you want to manipulate the individual document like adding tokens etc. Use the statics approach if you want query the whole collection.

提交回复
热议问题