0

I want to rename the collection in my schema, but mongodb keeps using the old name.

I have been running my code with the schema name set to '__filters', but now I need to change the name to '__filter'. ( NOT plural )

When I create a filter, mongodb creates a '__filters' collection

This is how I had the original Schema set up, note plural 'filters'

// create the schema
const FiltersSchema = new Schema({

    test: {type: String, required: true},
})

module.exports = Filters = mongoose.model('__filters', FiltersSchema)

Now I want to make the name of the collection singular '__filter'. This is the new schema that I want to use: NOTE: ALL singular now

// create the schema
const FilterSchema = new Schema({

    test: {type: String, required: true},
})

module.exports = Filter = mongoose.model('__filter', FilterSchema)

Here is the code that I am using:

const Filter = require('./Filter');

createFilter = ( test ) => {
    return new Promise((resolve, reject) =>{

        var errors = {};

        Filter.findOne( {test:test} )
        .then(found => {

            if(found) {
                errors.err = {'inUse':'already created'};
                console.log(errors);
                reject(errors);
            } else {

                const newFilter = new Filter({
                    test: test
                });

                newFilter.save()
                .then(f => {
                    if(debugThis){
                        console.log(' ');
                        console.log(' created ' + JSON.stringify(f));
                        console.log(' ');
                    }
                    resolve(f);
                })
                .catch(err => {
                    errors.exception = {'save':err};
                    console.log(errors);
                    reject(errors);
                });

            }
        })
        .catch(err => {
            errors.exception = {'findOne':err};
            reject(errors);
        })

    });
};

it's almost like there is some cache somewhere that is keeping the older 'filters' schema around.

Is there something I need to clear?

I have even tried this, which didn't work either

let dbc = mongoose.connection.db;
dbc.collection('filters').rename('filter')
.then()
.catch(err =>{});

I closed DevStudio and restarted it.

I have created a new database in MongoDB

I restarted the MongoDB server service

Nothing seems to reset '__filters' to '__filter'

In desperation, I remove the _filter from the schema and it crashed and spit this out:

 TypeError: Cannot read property 'toLowerCase' of undefined
at pluralize 
 (\node_modules\mongoose-legacy-pluralize\index.js:85:13)

Does mongoose make names plural automatically ??

Well blow me down olive oil... mongoose makes stuff plural... how 'nice' of them to do that... I DON'T want plural names...

MLissCetrus
  • 423
  • 3
  • 21

1 Answers1

0

OMG... I can't believe this... 4 hours of my time wasted on this stupid stuff:

Why does mongoose always add an s to the end of my collection name

MLissCetrus
  • 423
  • 3
  • 21