-
-
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
Model.syncIndexes() fails with MongoError: a collection 'dbname.tablename' already exists
#10420
Comments
If you could provide a readable repro script, that would be very helpful. |
The above code runs fine. Copy it and change it to demonstrate your issue. @rideddy84 |
Thanks for your help. @IslandRhythms Lines 1384 to 1409 in c03cacb
|
What do you mean by |
I have been trying to solve this problem since I've updated from @5.11.19 to @5.12.8 and now with @5.13.2 it is still there. The error is introduced through mongodb@3.6 (or somewhere around this ver) and occurs on |
I think he, just like me, wants to force reindex on every app init. Because out-of-the-box, if the raw DB tables have indexes defined, mongoose ignores index settings from |
In my cases,
I want to ensure the production MongoDB has exactly same indexes with model definition codes. |
@Spown @rideddy84 the below script shows that const mongoose = require('mongoose');
const {Schema} = mongoose;
const schema = new Schema({ name: { type: String, unique: true } });
const Customer = mongoose.model('Customer', schema);
async function test() {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true
});
await mongoose.connection.dropDatabase();
await Customer.collection.createIndex({ age: 1 }); // Index is not in schema
Customer.collection.getIndexes().then(indexes => {
console.log(indexes);
});
// Will drop the 'age' index and create an index on `name`
await Customer.syncIndexes();
Customer.collection.getIndexes().then(indexes => {
console.log(indexes);
});
console.log('Done');
}
test(); I also tried running the below script repeatedly, no errors const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
const orderSchema = new Schema({
orderNo: {
type: String,
required: true,
},
createdBy: { type: String, required: true, ref: "User" },
createdAt: { type: Date, default: Date.now },
});
orderSchema.index({ createdAt: -1 });
orderSchema.index({ orderNo: 1 }, { unique: true });
orderSchema.virtual("review", {
ref: "Review",
localField: "_id",
foreignField: "order",
justOne: true,
});
orderSchema.set("toObject", { virtuals: true });
orderSchema.set("toJSON", { virtuals: true });
const name = "Order";
const collection = "orders";
const orders = mongoose.model(name, orderSchema, collection);
orders.syncIndexes().then(() => console.log('Done')); |
@vkarpov15 Thanks for review. But actually Mongoose cannot ignore because If fires "MongoError" without invoking callback that checks |
Can you please elaborate on what you mean by "If fires "MongoError" without invoking callback that checks NamespaceExists."? |
Line 405 in c03cacb
Ok. You are expecting NamespaceExists when createCollection raises exception.
But actually, |
well, and I get the
|
@Spown you're right, this issue only happens in MongoDB 3.2 and earlier. Thanks for pointing that out - that's why we ask for MongoDB version in the issue template :) |
@vkarpov15 I'm sorry for missing out the MongoDB version in my case. It is |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
run
Model.syncIndexes();
for existing model.If the current behavior is a bug, please provide the steps to reproduce.
https://gist.github.com/rideddy84/a189d839cbef093f719a0e850f2c5ff7.js
What is the expected behavior?
Indexes are created if they weren't before.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
"mongoose": "5.13.2"
Node.js v14.17.0
The text was updated successfully, but these errors were encountered: