-
-
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
Problem with blank spaces in collection names #15198
Comments
@yhojann-cl I tested your code and checked everything there. The renaming process works fine. The problem with your code is how you're selecting the collection names array. Test the snippet below (I tested it and it works):
|
The following script works as expected. The collection in MongoDB is renamed, the model name stays the same. Which is expected because const mongoose = require('mongoose');
const schema = new mongoose.Schema({
// Definition ...
});
(async function run() {
mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
await mongoose.connection.dropDatabase();
const model = mongoose.model('foo ', schema);
await model.init();
console.log('Models', Object.keys(mongoose.connection.models));
console.log('Collections', await mongoose.connection.listCollections());
// Rename bad collection names
for(let name of [
'foo',
'bar',
])
if(Object.keys(mongoose.connection.models).includes(`${name} `))
await mongoose.connection.db.collection(`${name} `).rename(name);
console.log('Models', Object.keys(mongoose.connection.models));
console.log('Collections', await mongoose.connection.listCollections());
})(); Output:
A couple potential explanations for the confusion here:
mongoose.deleteModel('foo ');
mongoose.model('foo', schema); |
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
Prerequisites
Mongoose version
8.9.5
Node.js version
22.12.0
MongoDB server version
2.3.4
Typescript version (if applicable)
No response
Description
Due to a coding error I have defined a collection name with a blank space like this:
When using the model to create the database queries I had not noticed this problem, but now that I have seen it I have decided to make the corresponding change to avoid conflicts in data migrations and in the Mongoose middleware.
To detect this problem I have reviewed it from mongoose and from mongosh:
I understand that mongoose is just a driver that translates between nodejs and mongodb, but I think it's a mistake that it allows creating collections with blank spaces understanding that it's not even capable of maintaining capital letters in the name of the collections, for example, if you want to allow blank spaces to create collections of type 'foo bar' you shouldn't allow a 'foo ' or ' bar'.
My problem arises when I try to solve this problem, I have started by mongosh running:
I try force override namespace using the second argument and works fine:
Now that I know it works I have decided to create a migration file so that my code can apply this change automatically in all deployed instances of my application without having to manually run commands on all databases, I have tried to create this:
But does not work, mongoose can not rename the collection, it seems that Mongoose thinks the collection already exists and skips it, does not generate any errors and does not rename it. What can I do?
Says:
I can't find much information about the function either: https://mongoosejs.com/docs/api/connection.html#Connection.prototype.db
The
db
property on mongoose is not the same property as mongodb since it is not possible to access the models directly, it is necessary to use.collection
and it is not possible to directly access therenameCollection
function either, so I think the db property should have its own documentation.Steps to Reproduce
Create a collection name with a end blank space and try rename without blank space from mongoose.
Expected Behavior
That the name of the collection be modified and the models in mongoose memory be updated with the new changes.
The text was updated successfully, but these errors were encountered: