Reputation: 39
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?
Upvotes: 3
Views: 1474
Reputation: 13
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
Upvotes: 1
Reputation: 427
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.
Upvotes: 7