Skip to content

Explanation of exporting createConnection and models is confusing in docs and maybe wrong. #14528

@auctormaximus

Description

@auctormaximus

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is due to a mistake or omission in the mongoosejs.com documentation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions