-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
IslandRhythms
added
the
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
label
Sep 16, 2021
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. |
My bad I misread your code |
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
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(); |
Fix will be in v6.0.8. For posterity, this issue is closely related to #7620 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 addingindex: 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.
The text was updated successfully, but these errors were encountered: