最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the difference between mongoose.Schema() and new mongoose.Schema()? - Stack Overflow

programmeradmin2浏览0评论

What is the difference between mongoose.Schema() and new mongoose.Schema()?

I used both the standards, and I think both do the same job. Does it affect in any way?

What is the difference between mongoose.Schema() and new mongoose.Schema()?

I used both the standards, and I think both do the same job. Does it affect in any way?

Share Improve this question asked Jan 23, 2020 at 1:52 Madhubala JayakumaranMadhubala Jayakumaran 391 silver badge7 bronze badges 3
  • 1 Are you sure that both do the same job? I dunno. Just asking too. It may be possible that they both do the same job. The new keyword there means instantiate a new object though – orimdominic Commented Jan 23, 2020 at 2:02
  • duplicate stackoverflow./questions/39719454/… – Shivam Pandey Commented Jan 23, 2020 at 7:49
  • 1 Does this answer your question? Creating Mongoose Schemas with or without 'new' keyword? – Shivam Pandey Commented Jan 23, 2020 at 7:50
Add a ment  | 

2 Answers 2

Reset to default 7

Looking at the docs, it should always be called with the new keyword, since Schema is a constructor.

Looking at the source code, it'll return a newly allocated object if you try to use it without the new keyword.

Both will work, but I would only use the first since it's the correct way. Following standard conventions will make your code easier to read; not only for others, but for you as well when you go back to it in 6 months.

Apart from the standard practice and documentation, the only difference that i have figured out is this : (VS code specific) VS code seems to recognise the model which are created using a schema(created using new ) . ie. MongoDB specific intellisense like query operations, CRUD methods seem to be working.

Note. Schemas created without new do work fine in all the cases but the CRUD related intellisense (at least for my case) do not work. Without new with new

发布评论

评论列表(0)

  1. 暂无评论