-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
unique indexes not being created with autoIndex=true #8786
Comments
Unless you changed the default MongoDB port, you have a typo: Running the script you provided with the my MongoDB port (27017) throws an error:
|
Yeah I changed the port. Well actually I have three MongoDB servers running for testing on 27017-19. Obviously you have to have MongoDB running on the configured port. I will update the OP so people don't get confused. |
I modified the script a little bit, so we start on a fresh database every time which may affect the test. Does this still pass silently? // 8786
const mongoose = require('mongoose');
async function main () {
mongoose.set('autoIndex', true);
await mongoose.connect('mongodb://localhost/test', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
autoIndex: true
});
console.log('Mongoose open');
await mongoose.connection.dropDatabase();
const testSchema = new mongoose.Schema({
name: { type: String, unique: true, required: true, index: true }
});
const Test = mongoose.model('Test', testSchema);
await Test.insertMany([{ name: 'x' }, { name: 'x' }, { name: 'x' }], { runValidators: true });
}
main()
.then(() => process.exit())
.catch((err) => { console.error(err); process.exit(); }); |
The error does not occur with the above script. If I remove the In my initial testing I was using mongo shell to drop the collection: What could be going on? Some type of caching? UPDATE: Also noticed just doing a
UPDATE 2: I was reading the [ensureIndex()] doc. For that method index creation errors are emitted as an "index" event so I added this to the initial version with errors just after creating
It shows there is an index creation error because duplicates exist:
But they don't exist at the time the connection opened or the Model is created. So maybe this some kind of race condition? Based on that hypothesis, this is another fix confirmed to work:
|
You might find that helpful: https://mongoosejs.com/docs/faq.html#unique-doesnt-work |
As a side note, most mongoose functions return a thenable, so you can await them without manually creating a promise. |
OK so this is not actually a bug. It's just complicated and not very well documented :(. |
@AbdelrahmanHafez out of interest we're you talking about this part from OP?:
I can just do this?:
UPDATE: Oh yeah you can ... Ugh all this time :). |
@sgpinkus yes you can do Here's the docs on |
Do you want to request a feature or report a bug?
Bug.
What is the current behavior?
Setting
unique
on schema field does not create a unique index in a wide range of cases.If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
I shouldn't need to do (1) and (2) (see above code) to have the index created. There is nothing mentioned about that in the docs that I can see. It's not intuitive and fragile. Model.init() is also described an alternative to autoIndex=true, not required for indexing.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
5.9.7
The text was updated successfully, but these errors were encountered: