Skip to content
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

Closed
2 tasks done
auctormaximus opened this issue Apr 16, 2024 · 4 comments · Fixed by #14564
Closed
2 tasks done
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@auctormaximus
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

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

const conn = mongoose.createConnection(process.env.MONGODB_URI);
conn.model('User', require('../schemas/user'));

module.exports = conn; 

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

const conn = mongoose.createConnection(process.env.MONGODB_URI);
const User = conn.model('User', require('../schemas/user'));

module.exports = User;

Is this the right way to do it?

I am using Mongoose 8.1.3, Node 18.12.1, MongoDB 6.0.3

@FaizBShah
Copy link
Contributor

FaizBShah commented Apr 20, 2024

I think the names fast.js and slow.js is not the most appropiate names, but its basically called slow.js because we are initializing one extra model. I don't think there's anything more deeper than that to it.

It says to export the connection

const conn = mongoose.createConnection(process.env.MONGODB_URI);
conn.model('User', require('../schemas/user'));

module.exports = conn;
But this gives a "Not a function" error when trying to use it in another file.

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 exports.User = User;

@auctormaximus
Copy link
Author

but its basically called slow.js because we are initializing one extra model.

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?

Can you show the code how you are using it in the other file?

const conn = require('../connections')

That gave me a not a function error but I just realized it works with import conn from '../connections' while exporting the model works with require().

Is this how to access the model then? const User = conn.models.User

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.

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 exports.User = User;

I export multiple with

module.exports = {
    User,
    SecondModel,
    ThirdModel
}

And then const { User } = require('../connections')

I would have only one file I import all models from and from all databases. Is this ok?

@FaizBShah
Copy link
Contributor

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?

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

Is this how to access the model then? const User = conn.models.User

You should access it using the .model() function like this - const User = conn.model('User')

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.

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

@vkarpov15
Copy link
Collaborator

I agree that fast.js and slow.js isn't the most clear naming in isolation. The idea is that you have 2 separate connections: one for fast operational queries, and one for slower analytics type queries. And you can import each connection separately and use them as necessary. While we don't recommend this pattern for all apps, it can be useful. See this blog post for the motivation.

"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 .model() with 1 arg to get the model you're trying to access. For example:

const fastConn = require('../db/fast'); const User = fastConn.model('User');

or, equivalently, const { User } = fastConn.models;. That's likely the easiest way to avoid having to export all the models.

@vkarpov15 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Apr 28, 2024
@vkarpov15 vkarpov15 added this to the 8.3.4 milestone Apr 28, 2024
vkarpov15 added a commit that referenced this issue May 6, 2024
docs(connections): improve description of connection creation patterns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants