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

Mongoose v6 modifies connection options object #14370

Closed
2 tasks done
BrettHoutz opened this issue Feb 22, 2024 · 4 comments
Closed
2 tasks done

Mongoose v6 modifies connection options object #14370

BrettHoutz opened this issue Feb 22, 2024 · 4 comments
Milestone

Comments

@BrettHoutz
Copy link

BrettHoutz commented Feb 22, 2024

Prerequisites

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

Mongoose version

6.12.6

Node.js version

20.10.0

MongoDB server version

6.0.4

Typescript version (if applicable)

No response

Description

mongoose.connect() sets the properties config, autoCreate, and autoIndex, which i understand to be mongoose-specific connection options. My application creates connections using both mongoose and mongodb and uses a common options object for both. When the options object is passed to mongodb.MongoClient.connect(), it has the properties that were added by mongoose, which causes an error from MongoClient.

Steps to Reproduce

const mongoose = require('mongoose');

const options = {
  connectTimeoutMS: 5000,
  socketTimeoutMS: 30000,
  readPreference: 'nearest',
};

mongoose.connect('mongodb://localhost:27017', options).then(() => {
  // config, autoCreate, and autoIndex are set in options
  console.log(options);
});

Expected Behavior

Since the options object is a set of static properties for configuring the behavior of Mongoose, i would expect that it not be modified at all.

@donoftime2018
Copy link

What I did was update mongoose to latest version 8.2.0. And it only shows the options listed in the options object.

#!/usr/bin/env node

const mongoose = require('mongoose');

const options = {
  connectTimeoutMS: 5000,
  socketTimeoutMS: 30000,
  readPreference: 'nearest'
};

mongoose.connect('mongodb://localhost:27017', options).then(() => {
  console.log(options); //latest version of mongoose only shows the options listed
});

@donoftime2018
Copy link

donoftime2018 commented Feb 25, 2024

If you want to keep it at mongoose version 6.12.6, then removing readPreference only shows the fields listed in options.

#!/usr/bin/env node

const mongoose = require('mongoose');

const options = {
  connectTimeoutMS: 10,
  socketTimeoutMS: 10,
  autoCreate: false,
  autoIndex: true,
  config: {autoIndex: true},
//   readPreference: 'nearest'
};

mongoose.connect('mongodb://localhost:27017', options).then(() => {
  console.log(options); //only shows the options listed
});

@donoftime2018
Copy link

What I did was update mongoose to latest version 8.2.0. And it only shows the options listed in the options object.

#!/usr/bin/env node

const mongoose = require('mongoose');

const options = {
  connectTimeoutMS: 5000,
  socketTimeoutMS: 30000,
  readPreference: 'nearest'
};

mongoose.connect('mongodb://localhost:27017', options).then(() => {
  console.log(options); //latest version of mongoose only shows the options listed
});

I understand you wanted to not set other fields only on mongoose v6.

@vkarpov15
Copy link
Collaborator

This is a duplicate of #13335, fixed by cherry picking #13376.

@vkarpov15 vkarpov15 added this to the 6.12.7 milestone Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants