-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explanation of exporting createConnection and models is confusing in docs and maybe wrong. #14528
Comments
I think the names
Can you show the code how you are using it in the other file? Btw, your second approach is good if you are only planning to export one model. Otherwise you can also export multiple models too by doing |
Is there any performance issue of inititializing more than one model? Is it faster to use multiple files with each one only initializing one model?
That gave me a not a function error but I just realized it works with Is this how to access the model then? Is there any downside of directly exporting the model? I would rather do it that way as it's the model I want to use and not necessarily the connection itself.
I export multiple with
And then const I would have only one file I import all models from and from all databases. Is this ok? |
I don't think there will be any extra performance issue as all the models will be initialized anyways in both the methods. You can still try measuring the time taken in both the approaches
You should access it using the
Since you are using only one connection, so it should not be an issue, atleast according to me. But if you are using more than one connections, it might cause issue if models in more than one connection are named similarly, and it might become confusing |
I agree that "But this gives a "Not a function" error when trying to use it in another file." <-- what is "it" in this case? The way to use an exported connection is to call const fastConn = require('../db/fast'); const User = fastConn.model('User'); or, equivalently, |
docs(connections): improve description of connection creation patterns
Prerequisites
Issue
In connection in the section of exporting models with createConnection there is a fast.js and slow.js example. Why is the other slower? Should we always use the faster way? Should we separate models in different files for the same connection? This is not explained.
Here is the possible wrong part:
It says to export the connection
But this gives a "Not a function" error when trying to use it in another file.
However I got it to work by exporting the model with
Is this the right way to do it?
I am using Mongoose 8.1.3, Node 18.12.1, MongoDB 6.0.3
The text was updated successfully, but these errors were encountered: