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

Indexes are not created when we set readPreference to primary #10855

Closed
gemyero opened this issue Oct 8, 2021 · 1 comment · Fixed by #10861
Closed

Indexes are not created when we set readPreference to primary #10855

gemyero opened this issue Oct 8, 2021 · 1 comment · Fixed by #10861
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@gemyero
Copy link
Contributor

gemyero commented Oct 8, 2021

Do you want to request a feature or report a bug?
I want to report a bug.

What is the current behavior?
Suppose I have a replicaset with two nodes 1 primary and 1 secondary.

Primary: localhost:27017
Secondary: localhost:27018

When I connect using mongoose to the primary node using the following connection uri
mongodb://user:password@localhost:27017/dbName?authSource=admin&readPreference=primary

When I setup indexes on schema, it will not be created!

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');

mongoose.set('debug', true);

const mongoUrl =
  'mongodb://user:password@localhost:27017/dbName?authSource=admin&readPreference=primary';

const userSchema = new mongoose.Schema({
  name: { type: String, required: true },
});

userSchema.index({ email: 1 }, { unique: true });

const User = mongoose.model('User', userSchema);

(async () => {
  await mongoose.connect(mongoUrl);
  console.log('Connected...');
})();

What is the expected behavior?

Indexes should be created successfully as I provide readPreference as primary which is the default one. If I omit readPreference from options indexes will be created successfully, I know that readPreference=primary is the default but we should handle this also.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
MongoDB: 4.4.9
Node.js: v14.17.5
Mongoose: 6.0.8

@gemyero
Copy link
Contributor Author

gemyero commented Oct 8, 2021

#10861

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Oct 11, 2021
@vkarpov15 vkarpov15 added this to the 6.0.11 milestone Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants