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

Setting "unique: false" in a schema seems to automatically imply "index: true" #10738

Closed
sean-daley opened this issue Sep 16, 2021 · 5 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@sean-daley
Copy link

Do you want to request a feature or report a bug?
Report a potential bug

What is the current behavior?
If I set "unique: false" on an attribute in a schema, the collection that gets created is automatically getting an index for that attribute. Even when I do not set "index: true"

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

const mongoose = require("mongoose");

const { Schema } = mongoose;

const userSchema = new Schema({
  email: {
    type: Schema.Types.String,
    required: true
  },
  username: {
    type: Schema.Types.String,
    unique: false,
    required: true
  },
});

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

const main = async () => {
   const client = await mongoose.connect('mongodb://localhost:27017/test', {
  connectTimeoutMS: 3000
});
  const user = new User({
    username: "username",
    email: "user@example.com",
  });

  const dbUser = await user.save();
  console.log(dbUser.toObject());
  client.disconnect();
}

main();

If you do a db.users.getIndices() after doing this you will see a regular index on username.
If you change the schema by either removing unique: false or adding index: false then the index goes away.

What is the expected behavior?
It would seem like just having "unique: false" without the other attributes should not generate an index automatically.

Our primary use for doing this is we have helper methods we use to generate all of our schema attributes. These methods take true/false values for things like trim, unique, lowercase, etc. If we have to, we can try and go and fix our schemas to avoid setting unique at all instead of putting unique: false but it seems like this might just be a bug?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: v14.17.2
Mongoose: 6.0.6
Mongodb: 4.2.13

Note, this has been happening for a long while now (so with older versions of mongoose 5.x as well). So this behavior isn't new.

@IslandRhythms
Copy link
Collaborator

@IslandRhythms IslandRhythms added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Sep 16, 2021
@sean-daley
Copy link
Author

https://mongoosejs.com/docs/api.html#schematypeoptions_SchemaTypeOptions-unique

I've read that documentation but it says "when truthy it will build a unique index on ...". I'm not passing in a truthy value though.

@IslandRhythms
Copy link
Collaborator

My bad I misread your code

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Sep 16, 2021
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Sep 16, 2021

const mongoose = require("mongoose");

const { Schema } = mongoose;

const userSchema = new Schema({
  email: {
    type: Schema.Types.String,
    required: true
  },
  username: {
    type: Schema.Types.String,
    unique: false,
    required: true
  },
});

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

const main = async () => {
   const client = await mongoose.connect('mongodb://localhost:27017/test', {
  connectTimeoutMS: 3000
});
await mongoose.connection.dropDatabase();
  const user = new User({
    username: "username",
    email: "user@example.com",
  });

  const dbUser = await user.save();
  console.log(dbUser.toObject());
  console.log(await User.listIndexes())
  client.disconnect();
}

main();

@vkarpov15 vkarpov15 added this to the 6.0.9 milestone Sep 18, 2021
vkarpov15 added a commit that referenced this issue Sep 25, 2021
@vkarpov15
Copy link
Collaborator

Fix will be in v6.0.8.

For posterity, this issue is closely related to #7620

@vkarpov15 vkarpov15 modified the milestones: 6.0.9, 6.0.8 Sep 25, 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

No branches or pull requests

3 participants